home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / cpphtp2.jar / chpt_13.gml < prev    next >
Text File  |  1998-03-03  |  128KB  |  3,272 lines

  1. <html>
  2. <chapter>
  3. <section type=Popup name=Quotes title="Quotes">
  4. <page>
  5. <i>I never forget a face, but 
  6. in your case I'll make an 
  7. exception. </i>  <br>
  8. Groucho (Julius Henry) 
  9. Marx<br>
  10. <br>
  11.  
  12. </page>
  13. <page>
  14. <i>No rule is so general, 
  15. which admits not some 
  16. exception.</i> <br>
  17. Robert Burton, The 
  18. Anatomy of Melancholy<br>
  19. <br>
  20.  
  21. </page>
  22. <page>
  23. <i>It is common sense to take 
  24. a method and try it. If it 
  25. fails, admit it frankly and 
  26. try another. But above all, 
  27. try something.</i> <br>
  28. Franklin Delano 
  29. Roosevelt<br>
  30. <br>
  31.  
  32. </page>
  33. <page>
  34. <i>O! throw away the worser 
  35. part of it,                        
  36. And live the purer with 
  37. the other half.</i> <br>
  38. William Shakespeare<br>
  39. <br>
  40.  
  41. </page>
  42. <page>
  43. <i>If they're running and 
  44. they don't look where 
  45. they're going                      
  46. I have to come out from 
  47. somewhere and catch 
  48. them.</i> <br>
  49. Jerome David Salinger<br>
  50. <br>
  51.  
  52. </page>
  53. <page>
  54. <i>And oftentimes excusing 
  55. of a fault                      
  56. Doth make the fault the 
  57. worse by the excuse.</i>  <br>
  58. William Shakespeare<br>
  59. <br>
  60.  
  61. </page>
  62. <page>
  63. <i>To err is human, to 
  64. forgive divine.</i>  <br>
  65. Alexander Pope, An 
  66. Essay on Criticism<br>
  67. <br>
  68.  
  69. </page>
  70. </section>
  71. <section type=Popup name=Answers title="Answers">
  72. <page pagename="Answer 13.1">
  73. <b>Answer 13.1</b><br>
  74. Insufficient memory to satisfy a <tt><b>new</b></tt> request, array subscript out of bounds, 
  75. arithmetic overflow, division by zero, invalid function parameters.<br>
  76. <foreign  name="exercises" url="^Exercises::c:s0p0">
  77. <br>
  78.  
  79. </page>
  80. <page pagename="Answer 13.2">
  81. <b>Answer 13.2</b><br>
  82. a) Exception handling is designed to handle infrequently occurring situations that 
  83. often result in program termination, so compiler writers are not required to 
  84. implement exception handling to perform optimally.<br>
  85. b) Flow of control with conventional control structures is generally clearer and 
  86. more efficient than with exceptions.<br>
  87. c) Problems can occur because the stack is unwound when an exception occurs 
  88. and resources allocated prior to the exception may not be freed.<br>
  89. d) The "additional" exceptions can get in the way of genuine error-type 
  90. exceptions. It becomes more difficult for the programmer to keep track of the 
  91. larger number of exception cases. What does a <b>catch(...)</b> really catch?<br>
  92. <foreign  name="exercises" url="^Exercises::c:s0p1">
  93.  
  94. </page>
  95. <page pagename="Answer 13.3">
  96. <b>Answer 13.3</b><br>
  97. It is unlikely that a library function will perform error processing that will meet 
  98. the unique needs of all users.<br>
  99. <foreign  name="exercises" url="^Exercises::c:s0p2">
  100. <br>
  101. <br>
  102.  
  103. </page>
  104. <page pagename="Answer 13.4">
  105. <b>Answer 13.4</b><br>
  106. An aborting program could leave a resource in a state in which other programs 
  107. would not be able to acquire the resource.<br>
  108. <foreign  name="exercises" url="^Exercises::c:s0p3">
  109. <br>
  110. <br>
  111.  
  112. </page>
  113. <page pagename="Answer 13.5">
  114. <b>Answer 13.5</b><br>
  115. The exception handlers (in the <b>catch</b> blocks) for that <b>try</b> block are skipped and 
  116. the program resumes execution after the last <b>catch</b> block.<br>
  117. <foreign  name="exercises" url="^Exercises::c:s0p4">
  118. <br>
  119. <br>
  120.  
  121. </page>
  122. <page pagename="Answer 13.6">
  123. <b>Answer 13.6</b><br>
  124. An exception thrown outside a <b>try</b> block causes a call to <b>terminate</b>.<br>
  125. <foreign  name="exercises" url="^Exercises::c:s0p5">
  126. <br>
  127. <br>
  128.  
  129. </page>
  130. <page pagename="Answer 13.7">
  131. <b>Answer 13.7</b><br>
  132. The form <b>catch(...)</b> catches any type of error thrown in a <b>try</b> block. An advantage 
  133. is that no thrown error can slip by. A disadvantage is that the <b>catch</b> has no 
  134. parameter so it cannot reference information in the thrown object and cannot 
  135. know the cause of the error.<br>
  136. <foreign  name="exercises" url="^Exercises::c:s0p6">
  137. <br>
  138. <br>
  139.  
  140. </page>
  141. <page pagename="Answer 13.8">
  142. <b>Answer 13.8</b><br>
  143. This causes the search for a match to continue in the next enclosing <b>try</b> block. As 
  144. this process continues, it may eventually be determined that there is no handler in 
  145. the program that matches the type of the thrown object; in this case <b>terminate</b> is 
  146. called, which by default calls <b>abort</b>. An alternate <b>terminate</b> function can be 
  147. provided as an argument to <b>set_terminate</b>.<br>
  148. <foreign  name="exercises" url="^Exercises::c:s0p7">
  149. <br>
  150. <br>
  151.  
  152. </page>
  153. <page pagename="Answer 13.9">
  154. <b>Answer 13.9</b><br>
  155. The first matching exception handler after the <b>try</b> block is executed.<br>
  156. <foreign  name="exercises" url="^Exercises::c:s0p8">
  157. <br>
  158. <br>
  159.  
  160. </page>
  161. <page pagename="Answer 13.10">
  162. <b>Answer 13.10</b><br>
  163. This is a nice way to <b>catch</b> related types of exceptions.<br>
  164. <foreign  name="exercises" url="^Exercises::c:s0p9">
  165. <br>
  166. <br>
  167.  
  168. </page>
  169. <page pagename="Answer 13.11">
  170. <b>Answer 13.11</b><br>
  171. Provide a single exception class and <b>catch</b> handler for a group of exceptions. As 
  172. each exception occurs, the exception object can be created with different <b>private</b> 
  173. data. The <b>catch</b> handler can examine this <b>private</b> data to distinguish the type of 
  174. the exception.<br>
  175. <foreign  name="exercises" url="^Exercises::c:s0p10">
  176. <br>
  177. <br>
  178.  
  179. </page>
  180. <page pagename="Answer 13.12">
  181. <b>Answer 13.12</b><br>
  182. <font size=2><br></font><font size=11><pre>
  183. void *.<p>
  184. </pre></font>
  185. <foreign  name="exercises" url="^Exercises::c:s0p11">
  186. <br>
  187. <br>
  188.  
  189. </page>
  190. <page pagename="Answer 13.13">
  191. <b>Answer 13.13</b><br>
  192. A handler requiring standard conversions may appear before one with a precise 
  193. match.<br>
  194. <foreign  name="exercises" url="^Exercises::c:s0p12">
  195. <br>
  196. <br>
  197.  
  198. </page>
  199. <page pagename="Answer 13.14">
  200. <b>Answer 13.14</b><br>
  201. No, but it does terminate the block in which the exception is thrown.<br>
  202. <foreign  name="exercises" url="^Exercises::c:s0p13">
  203. <br>
  204. <br>
  205.  
  206. </page>
  207. <page pagename="Answer 13.15">
  208. <b>Answer 13.15</b><br>
  209. The exception will be processed by a <b>catch</b> handler (if one exists) associated with 
  210. the <b>try</b> block (if one exists) enclosing the <b>catch</b> handler that caused the 
  211. exception.<br>
  212. <foreign  name="exercises" url="^Exercises::c:s0p14">
  213. <br>
  214. <br>
  215.  
  216. </page>
  217. <page pagename="Answer 13.16">
  218. <b>Answer 13.16</b><br>
  219. It rethrows the exception.<br>
  220. <foreign  name="exercises" url="^Exercises::c:s0p15">
  221. <br>
  222. <br>
  223.  
  224. </page>
  225. <page pagename="Answer 13.17">
  226. <b>Answer 13.17</b><br>
  227. Provide an exception specification listing the exception types that can be thrown 
  228. from the function.<br>
  229. <foreign  name="exercises" url="^Exercises::c:s0p16">
  230. <br>
  231. <br>
  232.  
  233. </page>
  234. <page pagename="Answer 13.18">
  235. <b>Answer 13.18</b><br>
  236. Function <b>unexpected</b> is called.<br>
  237. <foreign  name="exercises" url="^Exercises::c:s0p17">
  238. <br>
  239. <br>
  240.  
  241. </page>
  242. <page pagename="Answer 13.19">
  243. <b>Answer 13.19</b><br>
  244. Through the process of stack unwinding, destructors are called for each of these 
  245. objects.<br>
  246. <foreign  name="exercises" url="^Exercises::c:s0p18">
  247. <br>
  248. <br>
  249.  
  250. </page>
  251. <page pagename="Answer 13.21">
  252. <b>Answer 13.21</b><br>
  253. If there is no information in the object that is required in the handler, a parameter 
  254. name is not required in the handler.<br>
  255. <foreign  name="exercises" url="^Exercises::c:s0p20">
  256. <br>
  257.  
  258. </page>
  259. <page pagename="Answer 13.27">
  260. <b>Answer 13.27</b><br>
  261. Create a base class for all related exceptions. In the base class, derive all the 
  262. related exception classes. Once the exception class hierarchy is created, 
  263. exceptions from the hierarchy can be caught as the base class exception type or as 
  264. one of the derived class exception types.<br>
  265. <foreign  name="exercises" url="^Exercises::c:s0p26">
  266. <br>
  267.  
  268. </page>
  269. <page pagename="Answer 13.29">
  270. <b>Answer 13.29</b><br>
  271. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  272. the file cpphtp2/answers/P13_29.zip to your hard drive and unzip the program 
  273. code.<br>
  274. <foreign  name="exercises" url="^Exercises::c:s0p28">
  275. <br>
  276.  
  277. </page>
  278. <page pagename="Answer 13.31">
  279. <b>Answer 13.31</b><br>
  280. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  281. the file cpphtp2/answers/P13_31.zip to your hard drive and unzip the program 
  282. code.<br>
  283. <foreign  name="exercises" url="^Exercises::c:s0p30">
  284. <br>
  285.  
  286. </page>
  287. <page pagename="Answer 13.34">
  288. <b>Answer 13.34</b><br>
  289. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  290. the file cpphtp2/answers/P13_34.zip to your hard drive and unzip the program 
  291. code.<br>
  292. <foreign  name="exercises" url="^Exercises::c:s0p33">
  293. <br>
  294.  
  295. </page>
  296. <page pagename="Answer 13.40">
  297. <b>Answer 13.40</b><br>
  298. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  299. the file cpphtp2/answers/P13_40.zip to your hard drive and unzip the program 
  300. code.<br>
  301. <foreign  name="exercises" url="^Exercises::c:s0p39">
  302. <br>
  303.  
  304. </page>
  305. <page pagename="Answer 13.42">
  306. <b>Answer 13.42</b><br>
  307. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  308. the file cpphtp2/answers/P13_42.zip to your hard drive and unzip the program 
  309. code.<br>
  310. <foreign  name="exercises" url="^Exercises::c:s0p41">
  311. <br>
  312.  
  313. </page>
  314. </section>
  315. <section type=Body name=Default title="13 Exception Handling">
  316. <page>
  317. <font size=18 bold>13 Exception Handling</font><hr>
  318. <a href="#s1p0">13.1<spacer width=20 height=1>Introduction</a>  <br>
  319. <a href="#s2p0">13.2<spacer width=20 height=1>When Exception Handling Should Be Used</a>  <br>
  320. <a href="#s3p0">13.3<spacer width=20 height=1>Other Error-Handling Techniques</a>  <br>
  321. <a href="#s4p0">13.4<spacer width=20 height=1>Basics of C++ Exception Handling: try, throw, 
  322. catch</a>  <br>
  323. <a href="#s5p0">13.5<spacer width=20 height=1>A Simple Exception-Handling Example: Divide by 
  324. Zero</a>  <br>
  325. <a href="#s6p0">13.6<spacer width=20 height=1>Throwing an Exception</a>  <br>
  326. <a href="#s7p0">13.7<spacer width=20 height=1>Catching an Exception</a>  <br>
  327. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  328. <foreign  name="quotes" url="^Quotes::c:s0p0">
  329.  
  330. </page>
  331. <page>
  332. <a href="#s8p0">13.8<spacer width=20 height=1>Rethrowing an Exception</a>  <br>
  333. <a href="#s9p0">13.9<spacer width=20 height=1>Exception Specifications</a>  <br>
  334. <a href="#s10p0">13.10<spacer width=20 height=1>Processing Unexpected Exceptions</a>  <br>
  335. <a href="#s11p0">13.11<spacer width=20 height=1>Stack Unwinding</a>  <br>
  336. <a href="#s12p0">13.12<spacer width=20 height=1>Constructors, Destructors and Exception 
  337. Handling</a>  <br>
  338. <a href="#s13p0">13.13<spacer width=20 height=1>Exceptions and Inheritance</a>  <br>
  339. <a href="#s14p0">13.14<spacer width=20 height=1>Processing new Failures</a>  <br>
  340. <a href="#s15p0">13.15<spacer width=20 height=1>Class <b>auto_ptr</b> and Dynamic Memory Allocation</a>  <br>
  341. <a href="#s16p0">13.16<spacer width=20 height=1>Standard Library Exception Hierarchy</a>  <br>
  342. <a href="#s17p0">13.17<spacer width=20 height=1>Summary</a>  <br>
  343. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  344. <foreign  name="quotes" url="^Quotes::c:s0p0">
  345.  
  346. </page>
  347. <page>
  348. <a href="^Terminology::c:s0p0">Terminology</a>  <br>
  349. <a href="^Illustration::c:s0p0">Figures</a>  <br>
  350. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  351. <foreign  name="quotes" url="^Quotes::c:s0p0">
  352.  
  353. </page>
  354. </section>
  355. <section type=Body name=Default title="13.1 Introduction">
  356. <page>
  357. <font size=18 bold>13.1 Introduction</font><hr>
  358. In this chapter, we introduce <i>exception handling</i>. The 
  359. extensibility of C++ can increase substantially the 
  360. number and kinds of errors that can occur. The features 
  361. presented here enable programmers to write clearer, 
  362. more robust, more fault-tolerant programs. Recent 
  363. systems developed with these and/or similar techniques 
  364. have reported positive results. We also mention when 
  365. exception handling should not be used.<br>
  366. <spacer width=16 height=1>The style and details of exception handling presented in 
  367. this chapter are based on the work of Andrew Koenig 
  368. and Bjarne Stroustrup as presented in their paper, <br>
  369.  
  370. </page>
  371. <page>
  372. "Exception Handling for C++ (revised)," published in 
  373. the <i>Proceedings of the USENIX C++ Conference</i> held 
  374. in San Francisco in April, 1990. <br>
  375. <spacer width=16 height=1>Error-handling code varies in nature and amount among 
  376. software systems depending on the application and 
  377. whether or not the software is a product for release. 
  378. Commercial products tend to contain far more error-
  379. handling code than "casual" software.<br>
  380. <spacer width=16 height=1>There are many popular means of dealing with errors. 
  381. Most commonly, error-handling code is interspersed 
  382. throughout a system's code. Errors are dealt with at the 
  383. places in the code where the errors can occur. The 
  384. advantage to this approach is that a programmer reading <br>
  385.  
  386. </page>
  387. <page>
  388. code can see the error processing in the immediate 
  389. vicinity of the code and determine if the proper error 
  390. checking has been implemented.<br>
  391. <spacer width=16 height=1>The problem with this scheme is that the code in a sense 
  392. becomes "polluted" with the error processing. It 
  393. becomes more difficult for a programmer concerned 
  394. with the application itself to read the code and 
  395. determine if the code is functioning correctly. This 
  396. makes it more difficult to understand and to maintain 
  397. the code.<br>
  398. <spacer width=16 height=1>Some common examples of exceptions are failure of 
  399. <b>new</b> to obtain a requested amount of memory, an out-<br>
  400.  
  401. </page>
  402. <page>
  403. of-bounds array subscript, arithmetic overflow, division 
  404. by zero, and invalid function parameters.<br>
  405. <spacer width=16 height=1>C++'s new exception-handling features enable the 
  406. programmer to remove the error-handling code from 
  407. the "main line" of a program's execution. This 
  408. improves program readability and modifiability. <br>
  409. <spacer width=16 height=1>With the C++ style of exception handling it is possible 
  410. to catch all kinds of exceptions, to catch all exceptions 
  411. of a certain type or to catch all exceptions of related 
  412. types. This makes programs more robust by reducing 
  413. the likelihood that errors will not be caught by a 
  414. program. Exception handling is provided to enable 
  415. programs to catch and handle errors rather than letting <br>
  416.  
  417. </page>
  418. <page>
  419. them occur and suffering the consequences. If the 
  420. programmer does not provide a means of handling a 
  421. fatal error, the program terminates.<br>
  422. <spacer width=16 height=1>Exception handling is designed for dealing with 
  423. <i>synchronous errors</i> such as an attempt to divide by zero 
  424. (that occurs as the program executes the divide 
  425. instruction). With exception handling, before the 
  426. program executes the division, it checks the 
  427. denominator and "throws" (issues) an exception if the 
  428. denominator is zero. <br>
  429. <spacer width=16 height=1>Exception handling is not designed to deal with 
  430. asynchronous situations such as disk I/O completions, 
  431. network message arrivals, mouse clicks, and the like; <br>
  432.  
  433. </page>
  434. <page>
  435. these are best handled through other means, such as 
  436. interrupt processing.<br>
  437. <spacer width=16 height=1>Exception handling is used in situations in which the 
  438. system can recover from the error causing the 
  439. exception. The recovery procedure is called an 
  440. <i>exception handler</i>.<br>
  441. <spacer width=16 height=1><a href="^Practice::c:s0p1"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>Exception handling is typically used in situations in 
  442. which the error will be dealt with by a different part of 
  443. the program (i.e., a different scope) from that which 
  444. detected the error. <a href="^Practice::c:s0p0"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>A program that carries on an 
  445. interactive dialog with a user should not use exceptions 
  446. to process input errors.<br>
  447.  
  448. </page>
  449. <page>
  450. Exception handling is especially appropriate for 
  451. situations in which the program will not be able to 
  452. recover, but needs to provide orderly cleanup, then shut 
  453. down "gracefully."<br>
  454. <spacer width=16 height=1><a href="^Engineer::c:s0p0"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>There is another reason to avoid using exception 
  455. handling techniques for conventional program control. 
  456. <a href="^Errors::c:s0p0"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Exception handling is designed for error processing 
  457. which is an infrequent activity that is often used 
  458. because a program is about to terminate. Given this, it 
  459. is not required that C++ compiler writers implement 
  460. exception handling for the kind of <a href="^Perform::c:s0p2"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>optimal performance 
  461. <a href="^Perform::c:s0p0"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>that might be expected of regular application code.<br>
  462.  
  463. </page>
  464. <page>
  465. Exception handling helps improve a program's fault 
  466. tolerance. It becomes "more pleasant" to write error-
  467. processing code, so programmers are more likely to 
  468. provide it. It also becomes possible to catch exceptions 
  469. in a variety of ways such as by type, or even to specify 
  470. that exceptions of any type are to be caught. <br>
  471. <spacer width=16 height=1>The majority of programs written today support only a 
  472. single thread of execution. Multithreading is receiving 
  473. great attention in recent operating systems like 
  474. Windows NT, OS/2, and various versions of UNIX. The 
  475. techniques discussed in this chapter apply even for 
  476. multithreaded programs, although we do not discuss 
  477. multithreaded programs specifically.<br>
  478.  
  479. </page>
  480. <page>
  481. We will show how to deal with "uncaught" exceptions. 
  482. We will consider how unexpected exceptions are 
  483. handled. We will show how related exceptions can be 
  484. represented by exception classes derived from a 
  485. common base exception class.<br>
  486. <spacer width=16 height=1>The exception-handling features of C++ are becoming 
  487. widely used as a result of the ANSI C++ 
  488. standardization effort.  <a href="^Engineer::c:s0p1"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Standardization is especially 
  489. important on large software projects where dozens or 
  490. even hundreds of people work on separate components 
  491. of a system and these components need to interact for 
  492. the overall system to function properly.<br>
  493.  
  494. </page>
  495. <page>
  496. Exception handling can be viewed as another means of 
  497. returning control from a function or exiting a block of 
  498. code. Normally, when an exception occurs, it will be 
  499. handled by a caller of the function generating the 
  500. exception, by a caller of that caller, or however far back 
  501. in the call chain it becomes necessary to go to find a 
  502. handler for that exception. <br>
  503.  
  504. </page>
  505. <page>
  506. <b>Select the true statement(s). </b><br>
  507. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Exception handling is designed for synchronous errors.">
  508. Exception handling is designed for asynchronous errors.   <br>
  509. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  510. Exception handling helps improve a program's fault tolerance.  <br>
  511. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  512. If the programmer does not provide the means of handling an exception, the program terminates. <br>
  513. <component type=button name=b label="Check Your Answer" width=125 height=24>
  514.  
  515. </page>
  516. </section>
  517. <section type=Body name=Default title="13.2 When Exception Handling Should Be Used">
  518. <page>
  519. <font size=18 bold>13.2 When Exception Handling Should Be 
  520. Used</font><hr>
  521. <a href="^Practice::c:s0p2"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>Exception handling should be used to process only 
  522. exceptional situations, despite the fact that there is 
  523. nothing to prevent the programmer from using 
  524. exceptions as an alternate for program control; to 
  525. process exceptions for program components that are not 
  526. geared to handling those exceptions directly; to process 
  527. exceptions from software components such as 
  528. functions, <a href="^Engineer::c:s0p3"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>libraries, and classes that are likely to be 
  529. widely used, and where it does not make sense for those 
  530. components to handle their own exceptions; on large <br>
  531.  
  532. </page>
  533. <page>
  534. projects to handle error processing in a uniform manner 
  535. project wide. <br>
  536.  
  537. </page>
  538. </section>
  539. <section type=Body name=Default title="13.3 Other Error-Handling Techniques">
  540. <page>
  541. <font size=18 bold>13.3 Other Error-Handling Techniques</font><hr>
  542. We have presented a variety of ways of dealing with 
  543. exceptional situations prior to this chapter. The 
  544. following summarizes these and other useful 
  545. techniques.<br>
  546. <indent width=8 delay>*   Use <b>assert</b> to test for coding and design errors. If an 
  547. assertion is <b>false</b>, the program terminates and the code 
  548. must be corrected. This is useful at debugging time.</indent>
  549. <indent width=8 delay>*  Simply ignore the exceptions. This would be devastating for software products released to the general public, or for special-purpose software needed for mission-
  550. critical situations. But for your own software developed </indent>
  551.  
  552. </page>
  553. <page>
  554. <indent width=8 delay>*   for your own purposes, it is quite common to ignore 
  555. many kinds of errors.</indent>
  556. <indent width=8 delay>*   Abort the program. This, of course, prevents a program from running to completion and producing incorrect results. Actually, for many types of errors this is a 
  557. good strategy, especially for nonfatal errors that enable 
  558. a program to run to completion, perhaps misleading the 
  559. programmer to think that the program functioned correctly. Here, too, such a strategy is inappropriate for 
  560. mission-critical applications. <a href="^Errors::c:s0p2"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Resource issues are also 
  561. important here. If a program obtains a resource, the program should normally return that resource before program termination. </indent>
  562.  
  563. </page>
  564. <page>
  565. <indent width=8 delay>*   Set some error indicator. The problem with this is 
  566. that programs may not check these error indicators at all 
  567. points at which the errors could be troublesome.</indent>
  568. <indent width=8 delay>*   Test for the error condition, issue an error message, 
  569. and call <b>exit</b> to pass an appropriate error code to the 
  570. program's environment.</indent>
  571. <indent width=8 delay>*  <b>setjump</b> and <b>longjump</b>. These capabilities, available 
  572. through <b><setjmp.h></b> enable the programmer to specify 
  573. an immediate jump out of deeply nested function calls 
  574. back to an error handler. Without <b>setjump</b>/<b>longjump</b>, a 
  575. program must execute several returns to get out of the 
  576. deeply nested function calls. These could certainly be 
  577. used to jump to some error handler. But they are dan</indent>
  578.  
  579. </page>
  580. <page>
  581. <indent width=8 delay>*   gerous in C++ because they unwind the stack without 
  582. calling destructors for automatic objects. This can lead 
  583. to serious problems.</indent>
  584. <indent width=8 delay>*   Certain specific kinds of errors have dedicated capabilities for handling them. For example, when <b>new</b> fails 
  585. to allocate memory, it can cause a <b>new_handler</b> function to execute to deal with the error. This function can 
  586. be varied by supplying a function name as the argument 
  587. to <b>set_new_handler</b>. We discuss function 
  588. <b>set_new_handler</b> in detail in<a href="#s14p0"> Section 13.14</a>.</indent>
  589.  
  590. </page>
  591. </section>
  592. <section type=Body name=Default title="13.4 Basics of C++ Exception Handling: try, throw, catch">
  593. <page>
  594. <font size=18 bold>13.4 Basics of C++ Exception Handling: <tt>try</tt>, 
  595. <tt>throw</tt>, <tt>catch
  596. </tt></font><hr>
  597. C++ exception handling is geared to situations in which 
  598. the function that detects an error is unable to deal with 
  599. it. Such a function will <b><i>throw</i></b> <i>an exception</i>. There is no 
  600. guarantee that there will be "anything out there"--i.e., 
  601. an <i>exception handler</i> specifically geared to processing 
  602. that kind of exception. If there is, the exception will be 
  603. <i>caught</i> and <i>handled</i>. If there is no exception handler for 
  604. that particular kind of exception, the program 
  605. terminates.<br>
  606.  
  607. </page>
  608. <page>
  609. The programmer encloses in a <b><i>try block</i></b> the code that 
  610. may generate an error that will produce an exception. 
  611. The <b>try</b> block is followed by one or more <b><i>catch blocks</i></b>. 
  612. Each <b>catch</b> block specifies the type of exception it can 
  613. catch and handle. Each <b>catch</b> block contains an 
  614. exception handler. If the exception matches the type of 
  615. the parameter in one of the <b>catch</b> blocks, the code for 
  616. that <b>catch</b> block is executed. Otherwise, the exception 
  617. mechanism looks for an exception handler in any 
  618. enclosing <b>try</b> block. If no handler is found, function 
  619. <b>terminate</b> is called, which by default calls function 
  620. <b>abort</b>.<br>
  621.  
  622. </page>
  623. <page>
  624. Program control on a thrown exception leaves the <b>try</b> 
  625. block and searches the <b>catch</b> blocks in order for an 
  626. appropriate handler (we will soon discuss what makes a 
  627. handler "appropriate"). If no exceptions are thrown in 
  628. the <b>try</b> block, the exception handlers for that block are 
  629. skipped and the program resumes execution after the 
  630. last <b>catch</b> block.<br>
  631. <spacer width=16 height=1>We can specify the exceptions a function <b>throw</b>s. As an 
  632. option, we can specify that a function shall not <b>throw</b> 
  633. any exceptions at all.<br>
  634. <spacer width=16 height=1><a href="^Engineer::c:s0p5"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>The exception is thrown in a <b>try</b> block in the function, 
  635. or the exception is thrown from a function called 
  636. directly or indirectly from the <b>try</b> block. The point at <br>
  637.  
  638. </page>
  639. <page>
  640. which the <b>throw</b> is executed is called the <b><i>throw point</i></b>. 
  641. This term is also used to describe the <b>throw</b> expression 
  642. itself. Once an exception is thrown, control cannot 
  643. return to the <b>throw</b> point.<br>
  644. <spacer width=16 height=1>When an exception occurs, it is possible to 
  645. communicate information to the exception handler from 
  646. the point of the exception. That information is the type 
  647. of the thrown object itself or information placed into 
  648. the thrown object.<br>
  649. <spacer width=16 height=1>The thrown object is typically a character string (for an 
  650. error message) or a class object. The thrown object 
  651. conveys information to the exception handler that will 
  652. process that exception. <br>
  653.  
  654. </page>
  655. <page>
  656. <b>Select the true statement(s). </b><br>
  657. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Any number of catch blocks may follow a try.">
  658. A try block can be followed with at most two catch blocks.   <br>
  659. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  660. Function terminate calls function abort.  <br>
  661. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  662. The point at which throw is executed is called the throw point. <br>
  663. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  664. Once an exception is thrown, control cannot continue from the throw point.  <br>
  665. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  666. The thrown object conveys information to the exception handler that will process that exception. <br>
  667. <component type=button name=b label="Check Your Answer" width=125 height=24>
  668.  
  669. </page>
  670. </section>
  671. <section type=Body name=Default title="13.5 A Simple Exception-Handling Example: Divide by Zero">
  672. <page>
  673. <font size=18 bold>13.5 A Simple Exception-Handling Example: Divide by Zero</font><hr>
  674. Now let us consider a simple example of exception 
  675. handling. The program of<a href="^Code::c:s0p0"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 13.1</a> uses <b>try</b>, <b>throw</b> and 
  676. <b>catch</b> to detect a division by zero, indicate a divide-by-
  677. zero exception and handle a divide-by-zero exception.<br>
  678. <spacer width=16 height=1>The first output window shows a successful execution. 
  679. In the second, a zero denominator is entered and the 
  680. program detects the error and issues an error message.<br>
  681. <spacer width=16 height=1>Now consider the driver program in <b>main</b>. The program 
  682. prompts for, and inputs, two integers. Note the 
  683. "localized" declaration of <b>number1</b> and <b>number2</b>. <br>
  684.  
  685. </page>
  686. <page>
  687. Next, the program proceeds with a <b>try</b> block which 
  688. wraps the code that may <b>throw</b> an exception. Note that 
  689. the actual division that may cause the error is not 
  690. explicitly listed inside the <b>try</b> block. Rather, the call to 
  691. function <b>quotient</b> invokes the code that attempts the 
  692. actual division. Function <b>quotient</b> actually <b>throw</b>s the 
  693. divide-by-zero exception object as we will see 
  694. momentarily. In general, errors may surface through 
  695. explicitly mentioned code in the <b>try</b> block, through 
  696. calls to a function, or even through deeply nested 
  697. function calls initiated by code in the <b>try</b> block. <br>
  698. <spacer width=16 height=1>The <b>try</b> block is immediately followed by a <b>catch</b> block 
  699. containing the exception handler for the divide-by-zero <br>
  700.  
  701. </page>
  702. <page>
  703. error. In general, when an exception is thrown within a 
  704. <b>try</b> block, the exception is caught by a <b>catch</b> block that 
  705. specifies the appropriate type that matches the thrown 
  706. exception. In <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 13.1</a>, the <b>catch</b> block specifies that it 
  707. will catch exception objects of type 
  708. <b>DivideByZeroException</b>; this type matches the type of 
  709. the object thrown in function <b>quotient</b>. The body of this 
  710. exception handler issues an error message and returns, 
  711. in this case with 1 indicating termination because of an 
  712. error. Exception handlers can be much more elaborate 
  713. than this. <br>
  714. <spacer width=16 height=1>If, when executed, the code in a <b>try</b> block does not 
  715. <b>throw</b> an exception, then all the <b>catch</b> handlers <br>
  716.  
  717. </page>
  718. <page>
  719. immediately following the <b>try</b> block are skipped and 
  720. execution resumes with the first line of code after the 
  721. <b>catch</b> handlers; in<a href="^Code::c:s0p0"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 13.1</a> a <b>return</b> statement is 
  722. executed that returns 0, indicating normal termination.<br>
  723. <spacer width=16 height=1>Now let us examine the definitions of class 
  724. <b>DivideByZeroException</b> and function <b>quotient</b>. In 
  725. function <b>quotient</b>, when the <b>if</b> statement determines 
  726. that the denominator is zero, the body of the <b>if</b> 
  727. statement issues a <b>throw</b> statement which specifies the 
  728. name of the constructor for the exception object. This 
  729. causes an object of class <b>DivideByZeroException</b> to 
  730. be created. This object will be caught by the <b>catch</b> 
  731. statement (specifying type <b>DivideByZeroException</b>) <br>
  732.  
  733. </page>
  734. <page>
  735. after the <b>try</b> block. The constructor for class 
  736. <b>DivideByZeroException</b> simply points data member 
  737. <b>message</b> at the string "<b>Divide by zero</b>". The thrown 
  738. <a href="^Practice::c:s0p3"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>object is received in the parameter specified in the 
  739. <b>catch</b> handler (in this case, parameter <b>error</b>), and the 
  740. message is printed there through a call to <b>public</b> access 
  741. function <b>printMessage</b>. <br>
  742.  
  743. </page>
  744. </section>
  745. <section type=Body name=Default title="13.6 Throwing an Exception">
  746. <page>
  747. <font size=18 bold>13.6 Throwing an Exception</font><hr>
  748. The <b>throw</b> keyword is used to indicate that an 
  749. exception has occurred. This is called <i>throwing an 
  750. exception</i>. A <b>throw</b> normally specifies one operand (a 
  751. special case we will discuss specifies no operands). The 
  752. operand of a <b>throw</b> can be of any type. If the operand is 
  753. an <i><a href="^Engineer::c:s0p7"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a></i>object, we call it an <i>exception object</i>. A conditional 
  754. expression can be thrown instead of an object. It is 
  755. possible to <b>throw</b> objects not intended for error 
  756. handling.<br>
  757. <spacer width=16 height=1>Where is an exception caught? Upon being thrown, the 
  758. exception will be caught by the closest exception <br>
  759.  
  760. </page>
  761. <page>
  762. handler (for the <b>try</b> block from which the exception was 
  763. thrown) specifying an appropriate type. The exception 
  764. handlers for a <b>try</b> block are listed immediately 
  765. following the <b>try</b> block.<br>
  766. <spacer width=16 height=1><a href="^Engineer::c:s0p9"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>As part of throwing an exception, a temporary copy of 
  767. the <b>throw</b> operand is created and initialized. This 
  768. temporary object then initializes the parameter in the 
  769. exception handler. The temporary object is destroyed 
  770. when the exception handler completes execution and 
  771. exits.<br>
  772. <spacer width=16 height=1>When an exception is thrown, control exits the current 
  773. <b>try</b> block and proceeds to an appropriate <b>catch</b> handler 
  774. (if one exists) after that <b>try</b> block. It is possible that the <br>
  775.  
  776. </page>
  777. <page>
  778. <b>throw</b> point could be in a deeply nested scope within a 
  779. <b>try</b> block; control will still proceed to the <b>catch</b> 
  780. handler. It is also possible that the <b>throw</b> point could be 
  781. in a deeply nested function call; still, control will 
  782. proceed to the <b>catch</b> handler.<br>
  783. <spacer width=16 height=1>A <b>try</b> block may appear to contain no error checking 
  784. and include no <b>throw</b> statements, but code referenced 
  785. in the <b>try</b> block could certainly cause error-checking 
  786. code in constructors to execute. Code in a <b>try</b> block 
  787. could perform array subscripting on an array class 
  788. object whose <b>operator[]</b> member function could be 
  789. overloaded to <b>throw</b> an exception on a subscript-out-
  790. of-range error. Any function call can invoke code that <br>
  791.  
  792. </page>
  793. <page>
  794. might <b>throw</b> an exception or call another function that 
  795. <b>throw</b>s an exception.<br>
  796. <spacer width=16 height=1><a href="^Errors::c:s0p6"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Although an exception can terminate program 
  797. execution, it is not required to terminate program 
  798. execution. However, an exception does terminate the 
  799. <a href="^Errors::c:s0p3"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>block in which the exception occurred.<br>
  800.  
  801. </page>
  802. <page>
  803. <b>Select the true statement(s). </b><br>
  804. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  805. As part of throwing an exception, a temporary copy of the throw operand is created and initialized.  <br>
  806. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. The operand can be of any data type.">
  807. The operand of throw can only be an object.   <br>
  808. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  809. The throw keyword is used to indicate that an exception has occured.  <br>
  810. <component type=button name=b label="Check Your Answer" width=125 height=24>
  811.  
  812. </page>
  813. </section>
  814. <section type=Body name=Default title="13.7 Catching an Exception">
  815. <page>
  816. <font size=18 bold>13.7 Catching an Exception</font><hr>
  817. Exception handlers are contained in <b>catch</b> blocks. Each 
  818. <b>catch</b> block starts with the keyword <b>catch</b> followed by 
  819. parentheses containing a type (indicating the type of 
  820. exception this catch block handles) and an optional 
  821. parameter name. This is followed by braces delineating 
  822. the exception-handling code. When an exception is 
  823. caught, the code in the <b>catch</b> block is executed.<br>
  824. <spacer width=16 height=1>The <b>catch</b> handler defines its own scope. A <b>catch</b> 
  825. <a href="^Errors::c:s0p7"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>specifies in parentheses the type of the object to be 
  826. caught. The parameter in a <b>catch</b> handler can be named 
  827. or unnamed. If the parameter is named, the parameter <br>
  828.  
  829. </page>
  830. <page>
  831. can be referenced in the handler. If the parameter is 
  832. unnamed, i.e., only a type is listed for purposes of 
  833. matching with the thrown object type, then information 
  834. is not conveyed from the <b>throw</b> point to the handler; 
  835. only control passes from the <b>throw</b> point to the handler. 
  836. For many exceptions, this is acceptable.<br>
  837. <spacer width=16 height=1>An exception whose thrown object's type matches the 
  838. type of the argument in the <b>catch</b> header causes the 
  839. <b>catch</b> block, i.e., the exception handler for exceptions 
  840. of that type, to execute.<br>
  841. <spacer width=16 height=1>The <b>catch</b> handler that catches an exception is the first 
  842. one listed after the currently active <b>try</b> block that <br>
  843.  
  844. </page>
  845. <page>
  846. matches the type of the thrown object. The matching 
  847. rules are discussed shortly.<br>
  848. <spacer width=16 height=1><a href="^Errors::c:s0p8"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>An exception that is not caught causes a call to 
  849. <b>terminate</b> which by default terminates a program by 
  850. calling <b>abort</b>. It is possible to specify customized 
  851. behavior by designating another function to be executed 
  852. by providing that function's name as the argument in a 
  853. <b>set_terminate</b> function call.<br>
  854. <spacer width=16 height=1><a href="^Engineer::c:s0p11"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>A <b>catch</b> followed by parentheses enclosing an ellipsis <br>
  855. <font size=2><br></font><font size=11><pre>
  856. catch( ... )<p>
  857. </pre></font>
  858. means to catch all exceptions. <br>
  859. <spacer width=16 height=1>It is possible that no handler will match a particular 
  860. thrown object. This causes the search for a match to <br>
  861.  
  862. </page>
  863. <page>
  864. continue in the next enclosing <b>try</b> block. As this 
  865. process continues, it may eventually be determined that 
  866. there is no handler in the program that matches the type 
  867. of the thrown object; in this case function <b>terminate</b> is 
  868. called, which by default calls function <b>abort</b>. <br>
  869. <spacer width=16 height=1>The exception handlers are searched in order for an 
  870. appropriate match. The first handler that yields a match 
  871. is executed. When that handler finishes executing, 
  872. control resumes with the first statement after the last 
  873. <b>catch</b> block, i.e., the first statement after the last 
  874. exception handler for that <b>try</b> block.<br>
  875. <spacer width=16 height=1>It is possible that several exception handlers will 
  876. provide an acceptable match to the type of the <br>
  877.  
  878. </page>
  879. <page>
  880. exception that was thrown. In this case, the first 
  881. exception handler that matches the exception type is 
  882. executed. If several handlers match, and if each of these 
  883. handles the exception differently, then the <a href="^Debug::c:s0p0"><img src="bckgrnds/icons/dbt_ico.gif" align=sidebar></a>order of the 
  884. handlers will affect the manner in which the exception 
  885. is handled.<br>
  886. <spacer width=16 height=1>It is possible that several <b>catch</b> handlers could contain a 
  887. class type that would match the type of a particular 
  888. thrown object. This can happen for several reasons. 
  889. First, there can be a "catch-all" handler <b>catch(...)</b> that 
  890. will catch any exception. <a href="^Errors::c:s0p9"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Second, because of 
  891. inheritance hierarchies, it is possible that a derived-
  892. class object can be caught either by a handler specifying <br>
  893.  
  894. </page>
  895. <page>
  896. the derived-class type, or by handlers specifying the 
  897. types of any base classes of that derived class.<br>
  898. <spacer width=16 height=1>Sometimes a program may process many closely 
  899. related types of exceptions. Instead of providing 
  900. separate exception classes and <b>catch</b> handlers for each, 
  901. a programmer can provide a single exception class and 
  902. <b>catch</b> handler for a group of exceptions. As each 
  903. exception occurs, the exception object can be created 
  904. with different <b>private</b> data. The <b>catch</b> handler can 
  905. examine this <b>private</b> data to distinguish the type of the 
  906. exception.<br>
  907.  
  908. </page>
  909. <page>
  910. When does a match occur? The type of the <b>catch</b> 
  911. handler parameter matches the type of the thrown 
  912. object if<br>
  913. <indent width=8 delay>*   they are indeed of the same type.</indent>
  914. <indent width=8 delay>*   the <b>catch</b> handler parameter type is a <b>public</b> base 
  915. class of the class of the thrown object. </indent>
  916. <indent width=8 delay>*   <a href="^Errors::c:s0p11"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>the handler parameter is of a base-class pointer or 
  917. reference type and the thrown object is of a derived-
  918. class pointer or reference type.</indent>
  919. <indent width=8 delay>*   the <b>catch</b> handler is of the form <b>catch( ... )</b>.</indent>
  920. An exact type match is required,. No promotions or 
  921. conversions are performed when looking for a handler 
  922. except for derived-class-to-base-class conversions.<br>
  923.  
  924. </page>
  925. <page>
  926. It is possible to <b>throw</b> <b>const</b> objects. In this case, the 
  927. <b>catch</b> handler argument type must also be declared 
  928. <b>const</b>.<br>
  929. <spacer width=16 height=1>By default, if no handler is found for an exception, the 
  930. program terminates. Although this may seem like the 
  931. right thing to do, it is not what programmers are 
  932. necessarily used to doing. Rather, errors often simply 
  933. happen and then program execution continues, possibly 
  934. only "hobbling" along.<br>
  935. <spacer width=16 height=1><a href="^Errors::c:s0p13"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>A <b>try</b> block followed by several <b>catche</b>s resembles a 
  936. <b>switch</b> statement. It is not necessary to use <b>break</b> to 
  937. exit an exception handler in a manner that skips over 
  938. the remaining exception handlers. Each <b>catch</b> block <br>
  939.  
  940. </page>
  941. <page>
  942. defines a distinct scope whereas all the cases in a 
  943. <b>switch</b> statement are contained within the scope of the 
  944. <b>switch</b>.<br>
  945. <spacer width=16 height=1>An exception handler cannot access automatic objects 
  946. defined within its <b>try</b> block because when an exception 
  947. occurs the <b>try</b> block terminates and all the automatic 
  948. objects inside the <b>try</b> block are destroyed before the 
  949. handler begins executing.<br>
  950. <spacer width=16 height=1>What happens when an exception occurs in an 
  951. exception handler? The original exception that was 
  952. caught is officially handled when the exception handler 
  953. begins executing. So exceptions occurring in an <br>
  954.  
  955. </page>
  956. <page>
  957. exception handler need to be processed outside the <b>try</b> 
  958. block in which the original exception was thrown.<br>
  959. <spacer width=16 height=1>Exception handlers can be written in a variety of ways. 
  960. They could take a closer look at an error and decide to 
  961. call <b>terminate</b>. They could <i>rethrow</i> an exception 
  962. (<a href="#s8p0">Section 13.8</a>). They could convert one type of 
  963. exception into another by throwing a different 
  964. exception. They could perform any necessary recovery 
  965. and resume execution after the last exception handler. 
  966. <a href="^Engineer::c:s0p16"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>They could look at the situation causing the error, 
  967. remove the cause of the error and retry by calling the 
  968. original function that caused an exception (this would <br>
  969.  
  970. </page>
  971. <page>
  972. not create infinite recursion). They could return some 
  973. status value to their environment, etc.<br>
  974. <spacer width=16 height=1>When a <b>try</b> block does not <b>throw</b> any exceptions and 
  975. the <b>try</b> block completes normal execution, control 
  976. passes to the first statement after the last <b>catch</b> handler 
  977. following the <b>try</b> block.<br>
  978. <spacer width=16 height=1><a href="^Errors::c:s0p14"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>It is not possible to return to the <b>throw</b> point by issuing 
  979. a <b>return</b> statement in a <b>catch</b> handler. Such a <b>return</b> 
  980. simply returns to the function that called the function 
  981. <a href="^Engineer::c:s0p13"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>containing the <b>catch</b> block.<br>
  982. <spacer width=16 height=1>When an exception is caught, it is possible that 
  983. resources may have been allocated but not yet released 
  984. in the <b>try</b> block. The <b>catch</b> handler, if possible, should <br>
  985.  
  986. </page>
  987. <page>
  988. release these resources. For example, the <b>catch</b> handler 
  989. should <b>delete</b> space allocated by <b>new</b> and should close 
  990. any files opened in the <b>try</b> block that threw the 
  991. exception. <br>
  992. <spacer width=16 height=1>A <b>catch</b> block can process the error in a manner that 
  993. enables the program to continue executing correctly. Or 
  994. the <b>catch</b> block can terminate the program.<br>
  995. <spacer width=16 height=1>A <b>catch</b> handler itself can discover an error and <b>throw</b> 
  996. an exception. <a href="^Errors::c:s0p15"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Such an exception will not be processed 
  997. by <b>catch</b> handlers associated with the same <b>try</b> block as 
  998. the <b>catch</b> handler throwing the exception. Rather the 
  999. thrown exception will be caught, if possible, by a <b>catch</b> 
  1000. handler associated with the next outer <b>try</b> block.<br>
  1001.  
  1002. </page>
  1003. <page>
  1004. <b>Select the true statement(s). </b><br>
  1005. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1006. An ellipsis inside a catch indicates that any exceptions can be caught by that exception handler.  <br>
  1007. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A catch block defines its own scope.">
  1008. A catch block cannot define its own scope.   <br>
  1009. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1010. After a catch block finishes executing, the statement after the last catch block of the try/catch sequence executes.  <br>
  1011. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. The catch block specifying a base class type will catch both base class objects and derived class objects.">
  1012. A catch block specifying a base class type will not catch a derived class exception object. <br>
  1013. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1014.  
  1015. </page>
  1016. </section>
  1017. <section type=Body name=Default title="13.8 Rethrowing an Exception">
  1018. <page>
  1019. <font size=18 bold>13.8 Rethrowing an Exception</font><hr>
  1020. It is possible that the handler that catches an exception 
  1021. may decide that it cannot process the exception or it 
  1022. may simply want to release resources before letting 
  1023. someone else handle it. In this case, the handler can 
  1024. simply rethrow the exception with the statement<br>
  1025. <font size=2><br></font><font size=11><pre>
  1026. throw;<p>
  1027. </pre></font>
  1028. <a href="^Errors::c:s0p17"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Such a <b>throw</b> with no arguments rethrows the 
  1029. exception. If no exception was thrown to begin with, 
  1030. then the rethrow causes a call to <b>terminate</b>. <br>
  1031. <spacer width=16 height=1><a href="^Engineer::c:s0p17"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Even if a handler can process an exception, and 
  1032. regardless of whether it does any processing on that <br>
  1033.  
  1034. </page>
  1035. <page>
  1036. exception, the handler can still rethrow the exception 
  1037. for further processing outside the handler.<br>
  1038. <spacer width=16 height=1>A rethrown exception is detected by the next enclosing 
  1039. <b>try</b> block and is handled by an exception handler listed 
  1040. after that enclosing <b>try</b> block.<br>
  1041. <spacer width=16 height=1>The program of <a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 13.2</a> demonstrates rethrowing an 
  1042. exception. In the <b>try</b> block at line 26 of <b>main</b>, function 
  1043. <b>throwException</b> is called. In the <b>try</b> block of function 
  1044. <b>throwException</b>, the <b>throw</b> statement at line 13 
  1045. <b>throw</b>s an instance of standard library class <b>exception</b> 
  1046. (defined in header file <tt><b><exception></b></tt>). This exception is 
  1047. caught immediately in the <b>catch</b> handler at line 15 
  1048. which prints an error message then rethrows the <br>
  1049.  
  1050. </page>
  1051. <page>
  1052. exception. This terminates function <b>throwException</b> 
  1053. and returns control to the <b>try<tt>/catch</tt></b> block in <b>main</b>. The 
  1054. exception is caught again at line 30 and an error 
  1055. message is printed.<br>
  1056.  
  1057. </page>
  1058. <page>
  1059. <b>Select the true statement(s). </b><br>
  1060. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1061. The statement throw; in a catch handler causes an exception to be rethrown.  <br>
  1062. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. An exception can be rethrown any number of times.">
  1063. An exception can only be rethrown once.   <br>
  1064. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1065. The C++ standard library provides class exception.  <br>
  1066. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1067.  
  1068. </page>
  1069. </section>
  1070. <section type=Body name=Default title="13.9 Exception Specifications">
  1071. <page>
  1072. <font size=18 bold>13.9 Exception Specifications</font><hr>
  1073. An <i>exception specification</i> enables a list of exceptions 
  1074. that can be thrown by a function to be specified.<br>
  1075. <font size=2><br></font><font size=11><pre>
  1076. int g( float h ) throw ( a, b, c )<p>
  1077. {<p>
  1078.    // function body<p>
  1079. }<p>
  1080. </pre></font>
  1081. It is possible to restrict the exception types thrown from 
  1082. a function. The exception types are specified in the 
  1083. function declaration as an <i>exception specification</i> (also 
  1084. called a <b>throw</b> list). The exception specification lists 
  1085. the exceptions that may be thrown. A function may 
  1086. <b>throw</b> the indicated exceptions or derived types. <br>
  1087.  
  1088. </page>
  1089. <page>
  1090. Despite this supposed guarantee that other exception 
  1091. types will not be thrown, it is possible to do so. If an 
  1092. exception not listed in the exception specification is 
  1093. thrown, function <b>unexpected</b> is called. <br>
  1094. <spacer width=16 height=1>Placing <b>throw()</b> (i.e., an <i>empty exception specification</i>) 
  1095. after a function's parameter list states that the function 
  1096. will not <b>throw</b> any exceptions. Such a function could, 
  1097. in fact, <b>throw</b> an <b><a href="^Errors::c:s0p18"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a></b>exception; this, too, would generate a 
  1098. call to <b>unexpected</b>.<br>
  1099. <spacer width=16 height=1>A function with no exception specification can <b>throw</b> 
  1100. any exception. <br>
  1101. <font size=2><br></font><font size=11><pre>
  1102. void g();      // this function can throw any exception<p>
  1103. </pre></font>
  1104.  
  1105. </page>
  1106. <page>
  1107. The meaning of the <b>unexpected</b> function can be 
  1108. redefined by calling function <b>set_unexpected</b>.<br>
  1109. <spacer width=16 height=1>One interesting aspect of exception handling is that the 
  1110. compiler will not consider it a syntax error if a function 
  1111. contains a <b>throw</b> expression for an exception not listed 
  1112. in the function's exception specification. The function 
  1113. must attempt to <b>throw</b> that exception at execution time 
  1114. before the error will be caught.<br>
  1115. <spacer width=16 height=1>If a function <b>throw</b>s an exception of a particular class 
  1116. type, that function can also <b>throw</b> exceptions of all 
  1117. classes derived from that class with <b>public</b> inheritance.<br>
  1118.  
  1119. </page>
  1120. <page>
  1121. <b>Select the true statement(s). </b><br>
  1122. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1123. Placing throw() after a function's parameter list states that a function will not throw any exceptions. <br>
  1124. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1125. A function's exception specification list specifies the exceptions a function can throw.  <br>
  1126. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Only one exception can be thrown at a time.">
  1127. A function can throw any number of exceptions at the same time.  <br>
  1128. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1129. If an exception is thrown that is not listed in the throw list, a call to unexpected occurs.  <br>
  1130. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1131.  
  1132. </page>
  1133. </section>
  1134. <section type=Body name=Default title="13.10 Processing Unexpected Exceptions">
  1135. <page>
  1136. <font size=18 bold>13.10 Processing Unexpected Exceptions</font><hr>
  1137. Function <b>unexpected</b> calls the function specified with 
  1138. the <b>set_unexpected</b> function. If no function has been 
  1139. specified in this manner, <b>terminate</b> is called by default.<br>
  1140. <spacer width=16 height=1>Function <b>terminate</b> can be called explicitly, if a thrown 
  1141. exception cannot be caught, if the stack is corrupted 
  1142. during exception handling, as the default action on a 
  1143. call to <b>unexpected</b>, and during stack unwinding 
  1144. initiated by an exception, an attempt by a destructor to 
  1145. <b>throw</b> an exception causes <b>terminate</b> to be called.<br>
  1146.  
  1147. </page>
  1148. <page>
  1149. Function <b>set_terminate</b> can specify the function that 
  1150. will be called when <b>terminate</b> is called. Otherwise, 
  1151. <b>terminate</b> calls <b>abort</b>.<br>
  1152. <spacer width=16 height=1>Prototypes for functions <b>set_terminate</b> and 
  1153. <b>set_unexpected</b> are found in header files 
  1154. <b><terminate.h></b> and <b><unexpected.h></b>, respectively.<br>
  1155. <spacer width=16 height=1>Function <b>set_terminate</b> and function <b>set_unexpected</b> 
  1156. each return a pointer to the last function called by 
  1157. <b>terminate</b> and <b>unexpected</b>. This enables the 
  1158. programmer to save the function pointer so it can be 
  1159. restored later.<br>
  1160. <spacer width=16 height=1>Functions <b>set_terminate</b> and <b>set_unexpected</b> take 
  1161. pointers to functions as arguments. Each argument must <br>
  1162.  
  1163. </page>
  1164. <page>
  1165. point to a function with <b>void</b> return type and no 
  1166. arguments.<br>
  1167. <spacer width=16 height=1>If the last action of a user-defined termination function 
  1168. is not to exit a program, function <b>abort</b> will 
  1169. automatically be called to end program execution after 
  1170. the other statements of the user-defined termination 
  1171. function are executed.<br>
  1172.  
  1173. </page>
  1174. </section>
  1175. <section type=Body name=Default title="13.11 Stack Unwinding">
  1176. <page>
  1177. <font size=18 bold>13.11 Stack Unwinding</font><hr>
  1178. When an exception is thrown but not caught in a 
  1179. particular scope, the function-call stack is unwound and 
  1180. an attempt is made to <b>catch</b> the exception in the next 
  1181. outer <b>try</b>/<b>catch</b> block. Unwinding the function-call 
  1182. stack means that the function in which the exception 
  1183. was not caught terminates, all local variables in that 
  1184. function are destroyed and control returns to the point at 
  1185. which that function was called. If that point in the 
  1186. program is in a <b>try</b> block, an attempt is made to <b>catch</b> 
  1187. the exception. If that point in the program is not in a <b>try</b> 
  1188. block or the exception is not caught, stack unwinding <br>
  1189.  
  1190. </page>
  1191. <page>
  1192. occurs again. As mentioned in the previous section, if 
  1193. the exception is not caught in the program, function 
  1194. <b>terminate</b> is called to terminate the program. The 
  1195. program of <a href="^Code::c:s0p2"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 13.3</a> demonstrates stack unwinding.<br>
  1196. <spacer width=16 height=1>In <b>main</b>, the <b>try</b> block at line 25 calls <b>function1</b>. Next, 
  1197. <b>function1</b> (defined at line 18) calls <b>function2</b>. Then, 
  1198. <b>function2</b> (defined at line 13) calls <b>function3</b>. Line 10 
  1199. of <b>function3</b> <b>throw</b>s an <b>exception</b> object. Because line 
  1200. 10 is not in a <b>try</b> block, stack unwinding occurs--
  1201. <b>function3</b> terminates and control returns to <b>function2</b> 
  1202. at line 15. Because line 15 is not in a <b>try</b> block, stack 
  1203. unwinding occurs again--<b>function2</b> terminates and 
  1204. control returns to <b>function1</b> at line 20. Because line 20 <br>
  1205.  
  1206. </page>
  1207. <page>
  1208. is not in a <b>try</b> block, stack unwinding occurs one more 
  1209. time--<b>function1</b> terminates and control returns to 
  1210. <b>main</b> at line 26. Because line 26 is in a <b>try</b> block, the 
  1211. <b>exception</b> can be caught and processed in the first 
  1212. matching <b>catch</b> handler after the <b>try</b> block (at line 28).<br>
  1213.  
  1214. </page>
  1215. </section>
  1216. <section type=Body name=Default title="13.12 Constructors, Destructors and Exception Handling">
  1217. <page>
  1218. <font size=18 bold>13.12 Constructors, Destructors and Exception Handling</font><hr>
  1219. First, let us deal with an issue we have mentioned, but 
  1220. that has yet to be satisfactorily resolved. What happens 
  1221. when an error is detected in a constructor? For example, 
  1222. how should a <b>String</b> constructor respond when <b>new</b> 
  1223. fails and indicates that it was unable to obtain the space 
  1224. needed to hold the <b>String</b>'s internal representation? The 
  1225. problem is that a constructor cannot return a value, so 
  1226. how do we let the outside world know that the object 
  1227. has not been properly constructed? One scheme was 
  1228. simply to return the improperly constructed object and <br>
  1229.  
  1230. </page>
  1231. <page>
  1232. hope that anyone using the object would make 
  1233. appropriate tests to determine that the object was in fact 
  1234. bad. Another scheme is to set some variable outside the 
  1235. constructor. A thrown exception passes to the outside 
  1236. world the information about the failed constructor and 
  1237. the responsibility to deal with the failure.<br>
  1238. <spacer width=16 height=1>To <b>catch</b> an exception, the exception handler must have 
  1239. access to a copy constructor for the thrown object 
  1240. (default memberwise copy is also valid). <br>
  1241. <spacer width=16 height=1>Exceptions thrown in constructors cause destructors to 
  1242. be called for any objects built as part of the object being 
  1243. constructed before the exception is thrown.<br>
  1244.  
  1245. </page>
  1246. <page>
  1247. Destructors are called for every automatic object 
  1248. constructed in a <b>try</b> block before an exception is 
  1249. thrown. An exception is handled at the moment the 
  1250. handler begins executing; stack unwinding is 
  1251. guaranteed to have been completed at that point. If a 
  1252. destructor invoked as a result of stack unwinding 
  1253. <b>throw</b>s an exception, <b>terminate</b> is called.<br>
  1254. <spacer width=16 height=1>If an object has member objects and if an exception is 
  1255. thrown before the outer object is fully constructed, then 
  1256. destructors will be executed for the member objects that 
  1257. have been constructed prior to the occurrence of the 
  1258. exception.<br>
  1259.  
  1260. </page>
  1261. <page>
  1262. If an array of objects has been partially constructed 
  1263. when an exception occurs, only the destructors for the 
  1264. constructed array elements will be called.<br>
  1265. <spacer width=16 height=1>An exception could preclude the operation of code that 
  1266. would normally release a resource, thus causing a 
  1267. <i>resource leak</i>. One technique to resolve this problem is 
  1268. to initialize a local object when the resource is acquired. 
  1269. When an exception occurs, the destructor will be 
  1270. invoked and can free the resource.<br>
  1271. <spacer width=16 height=1>It is possible to <b>catch</b> exceptions thrown from 
  1272. destructors by enclosing the function that calls the 
  1273. destructor in a <b>try</b> block and provide a <b>catch</b> handler 
  1274. with the proper type. The thrown object's destructor <br>
  1275.  
  1276. </page>
  1277. <page>
  1278. executes after an exception handler completes 
  1279. execution.<br>
  1280.  
  1281. </page>
  1282. <page>
  1283. <b>Select the true statement(s). </b><br>
  1284. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Destructors are called for every automatic object constructed in a try block before the exception is thrown.">
  1285. Destructors are not called for every automatic object constructed in a try block before the exception is thrown.   <br>
  1286. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1287. Exceptions thrown in constructors cause destructors to be called for any object built as part of the constructor call before the exception is thrown.   <br>
  1288. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1289.  
  1290. </page>
  1291. </section>
  1292. <section type=Body name=Default title="13.13 Exceptions and Inheritance">
  1293. <page>
  1294. <font size=18 bold>13.13 Exceptions and Inheritance</font><hr>
  1295. Various exception classes can be derived from a 
  1296. common <a href="^Debug::c:s0p2"><img src="bckgrnds/icons/dbt_ico.gif" align=sidebar></a>base class. If a <b>catch</b> catches a pointer or 
  1297. reference to an exception object of a base-class type, it 
  1298. can also <b>catch</b> a pointer or reference to all objects of 
  1299. classes derived from that base class. This can allow for 
  1300. polymorphic processing of related errors.<br>
  1301.  
  1302. </page>
  1303. </section>
  1304. <section type=Body name=Default title="13.14 Processing new Failures">
  1305. <page>
  1306. <font size=18 bold>13.14 Processing <tt>new</tt> Failures</font><hr>
  1307. There are several methods of dealing with <b>new</b> failures. 
  1308. To this point, we have used macro <b>assert</b> to test the 
  1309. value returned from <b>new</b>. If that value is <b>0</b>, the <b>assert</b> 
  1310. macro terminates the program. This is not a robust 
  1311. mechanism for dealing with <b>new</b> failures--it does not 
  1312. allow us to recover from the failure in any way. The 
  1313. ANSI/ISO C++ draft standard specifies that when <b>new</b> 
  1314. fails, it <b>throw</b>s a <b>bad_alloc</b> exception (defined in 
  1315. header file <tt><b><new></b></tt>). However, many compilers are not 
  1316. current with the draft standard and still use the version 
  1317. of <b>new</b> that returns <tt><b>0</b></tt> on failure. In this section we <br>
  1318.  
  1319. </page>
  1320. <page>
  1321. present three examples of <tt><b>new</b></tt> failing. The first 
  1322. example was compiled with Microsoft's Visual C++ 5.0 
  1323. which at the time of this publication still used the 
  1324. version of <b>new</b> that returns <b>0</b> when <b>new</b> fails. The 
  1325. second and third examples were compiled with the C++ 
  1326. compiler in Metrowerks CodeWarrior Professional 
  1327. Release 1 which uses the version of <b>new</b> that <b>throw</b>s a 
  1328. <b>bad_alloc</b> exception when <b>new</b> fails. We tested the 
  1329. three programs on separate computers running 
  1330. Windows 95. Each machine had different amounts of 
  1331. memory and hard disk space.<br>
  1332. <spacer width=16 height=1><a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 13.4</a> demonstrates <b>new</b> returning <b>0</b> on failure to 
  1333. allocate the requested amount of memory. The <b>for</b> <br>
  1334.  
  1335. </page>
  1336. <page>
  1337. structure at line 10 is supposed to loop 10 times and 
  1338. allocate an array of 5,000,000 <tt><b>double</b></tt> values (i.e., 
  1339. 40,000,000 bytes because a <tt><b>double</b></tt> is normally 8 
  1340. bytes) each time through the loop. The <b>if</b> structure at 
  1341. line 13 tests the result of each <b>new</b> operation to 
  1342. determine if the memory was allocated. If <b>new</b> fails and 
  1343. returns <tt><b>0</b></tt>, the message <tt><b>"Memory allocation failed\n"</b></tt> 
  1344. is printed and the loop terminates. <br>
  1345. <spacer width=16 height=1>The output shows that only two iterations of the loop 
  1346. were performed before <b>new</b> failed and the loop 
  1347. terminated. The output on your system may differ based 
  1348. on the physical memory and disk space you have 
  1349. available for virtual memory.<br>
  1350.  
  1351. </page>
  1352. <page>
  1353. <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 13.5</a> demonstrates <b>new</b> throwing <b>bad_alloc</b> 
  1354. when it fails to allocate the requested memory. The <b>for</b> 
  1355. structure at line 12 inside the <b>try</b> block is supposed to 
  1356. loop 10 times and on each pass allocate an array of 
  1357. 5,000,000 <tt><b>double</b></tt> values (i.e., 40,000,000 bytes 
  1358. because a <b>double</b> is normally 8 bytes). If <b>new</b> fails and 
  1359. <b>throw</b>s a <b>bad_alloc</b> exception, the loop terminates and 
  1360. the program continues in the exception-handling flow 
  1361. of control at line 18 where the exception is caught and 
  1362. processed. The message <tt><b>"Exception occurred: "</b></tt> is 
  1363. printed followed by <b>exception.what()</b> which returns a 
  1364. string with an exception-specific message (<tt><b>"Allocation 
  1365. Failure"</b></tt> in the case of <b>bad_alloc</b>). The output shows <br>
  1366.  
  1367. </page>
  1368. <page>
  1369. that only three iterations of the loop were performed 
  1370. before <b>new</b> failed and threw the <b>bad_alloc</b> exception. 
  1371. Your output may differ based on the physical memory 
  1372. and disk space available for virtual memory on your 
  1373. system. <br>
  1374. <spacer width=16 height=1>Compilers vary in the way they support <tt><b>new</b></tt> failure 
  1375. handling. In general, many current and older C++ 
  1376. compilers return <b>0</b> by default when <b>new</b> fails. Some of 
  1377. these compilers support new throwing an exception if 
  1378. the header file <b><new></b> (or <tt><b><new.h></b></tt>) is included. Other 
  1379. compilers--such as CodeWarrior Professional Release 
  1380. 1--<b>throw bad_alloc </b>by default whether or not you 
  1381. include header file <b><new></b>. Read the documentation for <br>
  1382.  
  1383. </page>
  1384. <page>
  1385. your compiler to determine your compiler's support for 
  1386. <b>new</b> failure handling.<br>
  1387. <spacer width=16 height=1><a href="^Engineer::c:s0p18"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>The ANSI/ISO C++ draft standard specifies that 
  1388. standard-compliant compilers can still use a version of 
  1389. <b>new</b> that returns <b>0</b> when it fails. For this purpose, the 
  1390. header file <b><new></b> defines the type <b>nothrow</b> that is 
  1391. used as follows:<br>
  1392. <font size=2><br></font><font size=11><pre>
  1393. double *ptr = new( nothrow ) double[ 5000000 ];<p>
  1394. </pre></font>
  1395. The preceding statement indicates that the version of 
  1396. <b>new</b> that does not <b>throw bad_alloc</b> exceptions (i.e., 
  1397. <b>nothrow</b>) should be used to allocate an array of 
  1398. 5,000,000 <b>double</b> values. <br>
  1399.  
  1400. </page>
  1401. <page>
  1402. There is an additional feature that can be used to 
  1403. perform handling of <b>new</b> failures. Function 
  1404. <b>set_new_handler</b> (prototyped in header file <b><new></b> or 
  1405. <b><new.h></b>) takes as its argument a function pointer for a 
  1406. function that takes no arguments and returns <b>void</b>. The 
  1407. function pointer is registered as the function to call 
  1408. when <b>new</b> fails. This provides the programmer with a 
  1409. uniform method of processing every <b>new</b> failure 
  1410. regardless of where the failure occurs in the program. 
  1411. Once a <b><i>new handler</i></b> is registered in the program with 
  1412. <b>set_new_handler</b>, <b>new</b> will not throw <b>bad_alloc</b> on 
  1413. failure. <br>
  1414.  
  1415. </page>
  1416. <page>
  1417. Operator <b>new</b> is actually a loop that attempts to acquire 
  1418. memory. If the memory is allocated, <b>new</b> returns a 
  1419. pointer to that memory. If <b>new</b> fails to allocate memory 
  1420. and no <b>new</b> handler function has been registered with 
  1421. <b>set_new_handler</b>, new throws a <b>bad_alloc</b> exception. 
  1422. If <b>new</b> fails to allocate memory and a <b>new</b> handler 
  1423. function has been registered, the <b>new</b> handler function 
  1424. is called. The C++ draft standard specifies that the <b>new</b> 
  1425. handler function should perform one of the following 
  1426. tasks:<br>
  1427.  
  1428. </page>
  1429. <page>
  1430. 1. Make more memory available by deleting other 
  1431. dynamically allocated memory and return to the loop in 
  1432. operator <b>new</b> to attempt to allocate the memory again.<br>
  1433. 2. Throw an exception of type <b>bad_alloc</b>.<br>
  1434. 3. Call function <b>abort</b> or<tt><b> exit</b></tt> (both from header file 
  1435. <b><cstdlib></b> or <tt><b><stdlib.h></b></tt>) to terminate the program.<br>
  1436. The program of <a href="^Code::c:s0p5"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 13.6</a> demonstrates 
  1437. <b>set_new_handler</b>. The function <b>customNewHandler</b> 
  1438. simply prints an error message and terminates the 
  1439. program with a call to <b>abort</b>. The output shows that 
  1440. only three iterations of the loop were performed before 
  1441. <b>new</b> failed and threw the <b>bad_alloc</b> exception. The 
  1442. output on your system may differ based on the physical <br>
  1443.  
  1444. </page>
  1445. <page>
  1446. memory and disk space you have available for virtual 
  1447. memory.<br>
  1448.  
  1449. </page>
  1450. <page>
  1451. <b>Select the true statement(s). </b><br>
  1452. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1453. ANSI/ISO draft standard C++ specifies that when new fails, it should throw a bad_alloc exception.   <br>
  1454. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Once a new handler is registered in the program with set_new_handler new will not throw a bad_alloc on failure.">
  1455. Once a new handler is registered in the program with set_new_handler, new will throw a bad_alloc on failure.   <br>
  1456. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1457.  
  1458. </page>
  1459. </section>
  1460. <section type=Body name=Default title="13.15 Class auto_ptr and Dynamic Memory Allocation">
  1461. <page>
  1462. <font size=18 bold>13.15 Class <b>auto_ptr</b> and Dynamic Memory 
  1463. Allocation</font><hr>
  1464. A common programming practice is to allocate 
  1465. dynamic memory (possibly an object) on the free store, 
  1466. assign the address of that memory to a pointer, use the 
  1467. pointer to manipulate the memory and deallocate the 
  1468. memory with <b>delete</b> when the memory is no longer 
  1469. needed. If an exception occurs after the memory has 
  1470. been allocated and before the <b>delete</b> statement is 
  1471. executed, a memory leak could occur. The ANSI/ISO 
  1472. C++ draft standard provides class template <b>auto_ptr</b> in 
  1473. header file <tt><b><memory></b></tt> to deal with this situation. <br>
  1474.  
  1475. </page>
  1476. <page>
  1477. An object of class <b>auto_ptr</b> maintains a pointer to 
  1478. dynamically allocated memory. When an <b>auto_ptr</b> 
  1479. object goes out of scope, it performs a <b>delete</b> operation 
  1480. on its pointer data member. Class template <b>auto_ptr</b> 
  1481. provides operators <b>*</b> and <b>-></b> so an <b>auto_ptr</b> object can 
  1482. be used like a regular pointer variable.<a href="^Code::c:s0p6"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 13.7</a> 
  1483. demonstrates an <b>auto_ptr</b> object that points to an object 
  1484. of class <b>Integer</b> (defined at lines 818). <br>
  1485. <spacer width=16 height=1>Line 25<br>
  1486. <font size=2><br></font><font size=11><pre>
  1487. auto_ptr< Integer > ptrToInteger( new Integer( 7 ) );<p>
  1488. </pre></font>
  1489.  
  1490. </page>
  1491. <page>
  1492. creates <b>auto_ptr</b> object <b>ptrToInteger</b> and initializes it 
  1493. with a pointer to a dynamically allocated <b>Integer</b> object 
  1494. containing the value 7. <br>
  1495. <spacer width=16 height=1>Line 28<br>
  1496. <font size=2><br></font><font size=11><pre>
  1497. ptrToInteger->setInteger( 99 );  <p>
  1498. </pre></font>
  1499. uses the <b>auto_ptr</b> overloaded <b>-></b> operator and the 
  1500. function call operator <b>() </b>to call function <b>setInteger</b> on 
  1501. the <b>Integer</b> object pointed to by <b>ptrToInteger</b>.<br>
  1502. <spacer width=16 height=1>The call<br>
  1503. <font size=2><br></font><font size=11><pre>
  1504. ( *ptrToInteger ).getInteger()<p>
  1505. </pre></font>
  1506. in line 30 uses the <b>auto_ptr</b> overloaded <b>*</b> operator to 
  1507. dereference <b>ptrToInteger</b> then uses the dot (<b>.</b>) operator <br>
  1508.  
  1509. </page>
  1510. <page>
  1511. and the function call operator <b>()</b> to call function 
  1512. <b>getInteger</b> on the <b>Integer</b> object pointer to by 
  1513. <b>ptrToInteger</b>.<br>
  1514. <spacer width=16 height=1>Because <b>ptrToInteger</b> is a local automatic variable in 
  1515. <b>main</b>, <b>ptrToInteger</b> is destroyed when <b>main</b> 
  1516. terminates. This forces a <b>delete</b> of the <b>Integer</b> object 
  1517. pointed to by <b>ptrToInteger</b> which, of course, forces a 
  1518. call to the <b>Integer</b> class destructor. Most importantly, 
  1519. this technique can prevent memory leaks.<br>
  1520.  
  1521. </page>
  1522. <page>
  1523. <b>Select the true statement(s). </b><br>
  1524. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1525. An object of class auto_ptr maintains a pointer to dynamically allocated memory.   <br>
  1526. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1527. Class template auto_ptr provides operators -> and *.   <br>
  1528. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. A delete operation will automatically be performed on the object to which the  points.">
  1529. When an auto_ptr goes out of scope, it will not perform a delete operation.auto_ptr   <br>
  1530. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1531.  
  1532. </page>
  1533. </section>
  1534. <section type=Body name=Default title="13.16 Standard Library Exception Hierarchy">
  1535. <page>
  1536. <font size=18 bold>13.16 Standard Library Exception Hierarchy</font><hr>
  1537. Experience has shown that exceptions fall nicely into a 
  1538. number of categories. The C++ draft standard includes 
  1539. a hi<a href="^Engineer::c:s0p19"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>erarchy of exception classes. This hierarchy is 
  1540. headed by base class <b>exception</b> (defined in header file 
  1541. <b><exception></b>) which offers the service <b>what()</b> that is 
  1542. overridden in each derived class to issue an appropriate 
  1543. error message.<br>
  1544. <spacer width=16 height=1>From base class <b>exception</b>, some of the immediate 
  1545. derived classes are <b>runtime_error</b> and <b>logic_error</b> 
  1546. (both defined in header <tt><b><stdexcept></b></tt>), each of which 
  1547. has several derived classes.<br>
  1548.  
  1549. </page>
  1550. <page>
  1551. <a href="^Errors::c:s0p19"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Also derived from <b>exception</b> are the exceptions thrown 
  1552. by C++ language features--for example, <b>bad_alloc</b> is 
  1553. thrown by <b>new</b> (<a href="#s14p0">Section 13.14</a>), <b>bad_cast</b> is thrown by 
  1554. <b>dynamic_cast</b> (Chapter 21) and <b>bad_typeid</b> is thrown 
  1555. by <b>typeid</b> (Chapter 21). By including 
  1556. <tt><b>std::bad_exception</b></tt> in the <b>throw</b> list of a function, if 
  1557. an unexpected exception occurs, <b>unexpected()</b> will 
  1558. throw <b>bad_exception</b> instead of terminating (by 
  1559. default) or instead of calling another function specified 
  1560. with <b>set_unexpected</b>.<br>
  1561. <spacer width=16 height=1>Class <b>logic_error</b> is the base class of several standard 
  1562. exception classes that indicate errors in program logic 
  1563. that can normally be prevented by writing proper code. <br>
  1564.  
  1565. </page>
  1566. <page>
  1567. Descriptions of some of these classes follow. Class 
  1568. <b>invalid_argument</b> indicates that an invalid argument 
  1569. was passed to a function (proper coding can, of course, 
  1570. prevent invalid arguments from reaching a function). 
  1571. Class <b>length_error</b> indicates that a length larger than 
  1572. the maximum size allowed for the object being 
  1573. manipulated was used for that object (we throw 
  1574. <b>length_errors</b> in Chapter 19 when we deal with 
  1575. <b>string</b>s). Class <b>out_of_range</b> indicates that a value 
  1576. such as a subscript into an array or <b>string</b> was out of 
  1577. range. <br>
  1578. <spacer width=16 height=1>Class <b>runtime_error</b> is the base class of several other 
  1579. standard exception classes that indicate errors in a <br>
  1580.  
  1581. </page>
  1582. <page>
  1583. program that can only be detected at execution time. 
  1584. Class <b>overflow_error</b> indicates that an arithmetic 
  1585. overflow error occurred. Class <b>underflow_error</b> 
  1586. indicates  <a href="^Debug::c:s0p5"><img src="bckgrnds/icons/dbt_ico.gif" align=sidebar></a>that an arithmetic underflow error occurred. <br>
  1587.  
  1588. </page>
  1589. </section>
  1590. <section type=Body name=Default title="13.17 Summary">
  1591. <page>
  1592. <font size=18 bold>13.17 Summary</font><hr>
  1593. <indent width=8 delay>*   Some common examples of exceptions are an out-of-
  1594. bounds array subscript, arithmetic overflow, division by 
  1595. zero, invalid function parameters and determining that 
  1596. there is insufficient memory to satisfy an allocation 
  1597. request by <b>new</b>.</indent>
  1598. <indent width=8 delay>*  The spirit behind exception handling is to enable programs to <b>catch</b> and handle errors rather than letting 
  1599. them occur and simply suffering the consequences. 
  1600. With exception handling, if the programmer does not 
  1601. provide a means of handling a fatal error, the program 
  1602. will <b>terminate</b>; nonfatal errors normally allow a pro</indent>
  1603.  
  1604. </page>
  1605. <page>
  1606. <indent width=8 delay>*   gram to continue executing, but produce incorrect 
  1607. results.</indent>
  1608. <indent width=8 delay>*   Exception handling is designed for dealing with synchronous errors, i.e., errors that occur as the result of a 
  1609. program's execution. </indent>
  1610. <indent width=8 delay>*   Exception handling is not designed to deal with asynchronous situations such as network message arrivals, 
  1611. disk I/O completions, mouse clicks, and the like; these 
  1612. are best handled through other means, such as interrupt 
  1613. processing.</indent>
  1614. <indent width=8 delay>*  Exception handling is typically used in situations in 
  1615. which the error will be dealt with by a different part of 
  1616. the program (i.e., a different scope) from that which </indent>
  1617.  
  1618. </page>
  1619. <page>
  1620. <indent width=8 delay>*   detected the error. </indent>
  1621. <indent width=8 delay>*   Exceptions should not be used as an alternate mechanism for specifying flow of control. Flow of control 
  1622. with conventional control structures is generally clearer 
  1623. and more efficient than with exceptions.</indent>
  1624. <indent width=8 delay>*   Exception handling should be used to process exceptions for program components that are not geared to 
  1625. handling those exceptions directly.</indent>
  1626. <indent width=8 delay>*   Exception handling should be used to process exceptions from software components such as functions, 
  1627. libraries, and classes that are likely to be widely used, 
  1628. and where it does not make sense for those components 
  1629. to handle their own exceptions.</indent>
  1630.  
  1631. </page>
  1632. <page>
  1633. <indent width=8 delay>*   Exception handling should be used on large projects 
  1634. to handle error processing in a uniform manner for the 
  1635. entire project.</indent>
  1636. <indent width=8 delay>*   C++ exception handling is geared to situations in 
  1637. which the function that detects an error is unable to deal 
  1638. with it. Such a function will <b>throw</b> an exception. If the 
  1639. exception matches the type of the parameter in one of 
  1640. the <b>catch</b> blocks, the code for that <b>catch</b> block is executed. Otherwise, function <b>terminate</b> is called, which 
  1641. by default calls function <b>abort</b>.</indent>
  1642. <indent width=8 delay>*  The programmer encloses in a <b>try</b> block the code that 
  1643. may generate an error that will produce an exception. 
  1644. The <b>try</b> block is immediately followed by one or more </indent>
  1645.  
  1646. </page>
  1647. <page>
  1648. <indent width=8 delay>*   <b>catch</b> blocks. Each <b>catch</b> block specifies the type of 
  1649. exception it can <b>catch</b> and handle. Each <b>catch</b> block 
  1650. contains an exception handler.</indent>
  1651. <indent width=8 delay>*   Program control on a thrown exception leaves the <b>try</b> 
  1652. block and searches the <b>catch</b> blocks in order for an 
  1653. appropriate handler. If no exceptions are thrown in the 
  1654. <b>try</b> block, the exception handlers for that block are 
  1655. skipped and the program resumes execution after the 
  1656. last <b>catch</b> block.</indent>
  1657. <indent width=8 delay>*   Exceptions are thrown in a <b>try</b> block in a function or 
  1658. from a function called directly or indirectly from the <b>try</b> 
  1659. block.</indent>
  1660. <indent width=8 delay>*  Once an exception is thrown, control cannot return </indent>
  1661.  
  1662. </page>
  1663. <page>
  1664. <indent width=8 delay>*   directly to the <b>throw</b> point.</indent>
  1665. <indent width=8 delay>*   It is possible to communicate information to the 
  1666. exception handler from the point of the exception. That 
  1667. information is the type of thrown object or information 
  1668. placed into the thrown object.</indent>
  1669. <indent width=8 delay>*   A popular exception type thrown is <b>char *</b>. It is common to simply include an error message as the operand 
  1670. of the <b>throw</b>.</indent>
  1671. <indent width=8 delay>*   The exceptions thrown by a particular function can 
  1672. be specified with an exception specification. An empty 
  1673. exception specification states that the function will not 
  1674. <b>throw</b> any exceptions.</indent>
  1675. <indent width=8 delay>*  Exceptions are caught by the closest exception han</indent>
  1676.  
  1677. </page>
  1678. <page>
  1679. <indent width=8 delay>*   dler (for the <b>try</b> block from which the exception was 
  1680. thrown) specifying an appropriate type. </indent>
  1681. <indent width=8 delay>*   As part of throwing an exception, a temporary copy 
  1682. of the <b>throw</b> operand is created and initialized. This 
  1683. temporary object then initializes the proper variable in 
  1684. the exception handler. The temporary object is 
  1685. destroyed when the exception handler is exited.</indent>
  1686. <indent width=8 delay>*   Errors are not always checked explicitly. A <b>try</b> block, 
  1687. for example, may appear to contain no error checking 
  1688. and include no <b>throw</b> statements. But code referenced 
  1689. in the <b>try</b> block could certainly cause error-checking 
  1690. code in constructors to execute. </indent>
  1691. <indent width=8 delay>*  An exception terminates the block in which the </indent>
  1692.  
  1693. </page>
  1694. <page>
  1695. <indent width=8 delay>*   exception occurred.</indent>
  1696. <indent width=8 delay>*   Exception handlers are contained in <b>catch</b> blocks. 
  1697. Each <b>catch</b> block starts with the keyword <b>catch</b> followed by parentheses containing a type and an optional 
  1698. parameter name. This is followed by braces delineating 
  1699. the exception-handling code. When an exception is 
  1700. caught, the code in the <b>catch</b> block is executed.</indent>
  1701. <indent width=8 delay>*   The <b>catch</b> handler defines its own scope. </indent>
  1702. <indent width=8 delay>*  The parameter in a <b>catch</b> handler can be named or 
  1703. unnamed. If the parameter is named, the parameter can 
  1704. be referenced in the handler. If the parameter is 
  1705. unnamed, i.e., only a type is listed for the purpose of 
  1706. matching with the thrown object type or an ellipsis for </indent>
  1707.  
  1708. </page>
  1709. <page>
  1710. <indent width=8 delay>*   all types, then the handler will ignore the thrown object. 
  1711. The handler may rethrow the object to an outer <b>try</b> 
  1712. block. </indent>
  1713. <indent width=8 delay>*   It is possible to specify customized behavior to 
  1714. replace function <b>terminate</b> by designating another 
  1715. function to be executed and providing that function's 
  1716. name as the argument in a <b>set_terminate</b> function call.</indent>
  1717. <indent width=8 delay>*   <b>catch(...)</b> means to <b>catch</b> all exceptions.</indent>
  1718. <indent width=8 delay>*   It is possible that no handler will match a particular 
  1719. thrown object. This causes the search for a match to 
  1720. continue in an enclosing <b>try</b> block.</indent>
  1721. <indent width=8 delay>*  The exception handlers are searched in order for an 
  1722. appropriate match. The first handler that yields a match </indent>
  1723.  
  1724. </page>
  1725. <page>
  1726. <indent width=8 delay>*   is executed. When that handler finishes executing, control resumes with the first statement after the last <b>catch</b> 
  1727. block.</indent>
  1728. <indent width=8 delay>*   The order of the exception handlers affects how an 
  1729. exception is handled.</indent>
  1730. <indent width=8 delay>*   A derived-class object can be caught either by a handler specifying the derived-class type or by handlers 
  1731. specifying the types of any base classes of that derived 
  1732. class.</indent>
  1733. <indent width=8 delay>*  Sometimes a program may process many closely 
  1734. related types of exceptions. Instead of providing separate exception classes and <b>catch</b> handlers for each, a 
  1735. programmer can provide a single exception class and </indent>
  1736.  
  1737. </page>
  1738. <page>
  1739. <indent width=8 delay>*   <b>catch</b> handler for a group of exceptions. As each exception occurs, the exception object can be created with 
  1740. different <b>private</b> data. The <b>catch</b> handler can examine 
  1741. this <b>private</b> data to distinguish the type of the exception.</indent>
  1742. <indent width=8 delay>*   It is possible that even though a precise match is 
  1743. available, a match requiring standard conversions will 
  1744. be made because that handler appears before the one 
  1745. that would result in a precise match. </indent>
  1746. <indent width=8 delay>*   By default, if no handler is found for an exception, 
  1747. the program terminates. </indent>
  1748. <indent width=8 delay>*  An exception handler cannot directly access variables in the scope of its <b>try</b> block. Information the han</indent>
  1749.  
  1750. </page>
  1751. <page>
  1752. <indent width=8 delay>*   dler needs is normally passed in the thrown object.</indent>
  1753. <indent width=8 delay>*   Exception handlers can take a closer look at an error 
  1754. and decide to call <b>terminate</b>. They can rethrow an 
  1755. exception. They can convert one type of exception into 
  1756. another by throwing a different exception. They can 
  1757. perform any necessary recovery and resume execution 
  1758. after the last exception handler. They can look at the situation causing the error, remove the cause of the error, 
  1759. and retry by calling the original function that caused an 
  1760. exception (this would not create infinite recursion). 
  1761. They can simply return some status value to their environment, etc.</indent>
  1762. <indent width=8 delay>*  A handler that catches a derived-class object should </indent>
  1763.  
  1764. </page>
  1765. <page>
  1766. <indent width=8 delay>*   be placed before a handler that catches a base-class 
  1767. object. If the base-class handler were first, it would 
  1768. catch both the base-class objects and the object of 
  1769. classes derived from that base class.</indent>
  1770. <indent width=8 delay>*   When an exception is caught, it is possible that 
  1771. resources may have been allocated but not yet released 
  1772. in the <b>try</b> block. The <b>catch</b> handler should release these 
  1773. resources. </indent>
  1774. <indent width=8 delay>*  It is possible that the handler that <b>catch</b>es an exception may decide that it cannot process the exception. In 
  1775. this case, the handler can simply rethrow the exception. 
  1776. A <b>throw</b> with no arguments rethrows the exception. If 
  1777. no exception was thrown to begin with, then the </indent>
  1778.  
  1779. </page>
  1780. <page>
  1781. <indent width=8 delay>*   rethrow causes a call to <b>terminate</b>.</indent>
  1782. <indent width=8 delay>*   Even if a handler can process an exception, and 
  1783. regardless of whether it does any processing on that 
  1784. exception, the handler can rethrow the exception for 
  1785. further processing outside the handler. A rethrown 
  1786. exception is detected by the next enclosing <b>try</b> block 
  1787. and is handled by an exception handler listed after that 
  1788. enclosing <b>try</b> block.</indent>
  1789. <indent width=8 delay>*   A function with no exception specification can 
  1790. <b>throw</b> any exception.</indent>
  1791. <indent width=8 delay>*   Function <b>unexpected</b> calls a function specified with 
  1792. function <b>set_unexpected</b>. If no function has been specified in this manner, <b>terminate</b> is called by default.</indent>
  1793.  
  1794. </page>
  1795. <page>
  1796. <indent width=8 delay>*   Function <b>terminate</b> can be called in various ways: 
  1797. explicitly; if a thrown exception cannot be caught; if the 
  1798. stack is corrupted during exception handling; as the 
  1799. default action on a call to <b>unexpected</b>; or if during 
  1800. stack unwinding initiated by an exception, an attempt 
  1801. by a destructor to <b>throw</b> an exception causes <b>terminate</b> 
  1802. to be called.</indent>
  1803. <indent width=8 delay>*   Prototypes for functions <b>set_terminate</b> and 
  1804. <b>set_unexpected</b> are found in header files <b><terminate.h></b> and <b><unexpected.h></b>, respectively.</indent>
  1805. <indent width=8 delay>*  Functions <b>set_terminate</b> and <b>set_unexpected</b> return 
  1806. pointers to the last function called, by <b>terminate</b> and 
  1807. <b>unexpected</b>. This enables the programmer to save the </indent>
  1808.  
  1809. </page>
  1810. <page>
  1811. <indent width=8 delay>*   function pointer so it can be restored later.</indent>
  1812. <indent width=8 delay>*   Functions <b>set_terminate</b> and <b>set_unexpected</b> take 
  1813. pointers to functions as arguments. Each argument must 
  1814. point to a function with <b>void</b> return type and no arguments.</indent>
  1815. <indent width=8 delay>*   If the last action of a user-defined termination function is not to exit a program, function <b>abort</b> will automatically be called to end program execution after the 
  1816. other statements of the user-defined termination function are executed.</indent>
  1817. <indent width=8 delay>*   An exception thrown outside a <b>try</b> block will cause 
  1818. the program to terminate.</indent>
  1819. <indent width=8 delay>*  If a handler cannot be found after a <b>try</b> block, stack </indent>
  1820.  
  1821. </page>
  1822. <page>
  1823. <indent width=8 delay>*   unwinding continues until an appropriate handler is 
  1824. found. If a handler is ultimately not found, then <b>terminate</b> is called which by default aborts the program with 
  1825. <b>abort</b>.</indent>
  1826. <indent width=8 delay>*   Exception specifications list the exceptions that may 
  1827. be thrown from a function. A function may <b>throw</b> the 
  1828. indicated exceptions, or it may <b>throw</b> derived types. If 
  1829. an exception not listed in the exception specification is 
  1830. thrown, <b>unexpected</b> is called. </indent>
  1831. <indent width=8 delay>*   If a function <b>throw</b>s an exception of a particular class 
  1832. type, that function can also <b>throw</b> exceptions of all 
  1833. classes derived from that class with <b>public</b> inheritance.</indent>
  1834. <indent width=8 delay>*  To <b>catch</b> an exception, the exception handler must </indent>
  1835.  
  1836. </page>
  1837. <page>
  1838. <indent width=8 delay>*   have access to a copy constructor for the thrown object.</indent>
  1839. <indent width=8 delay>*   Exceptions thrown from constructors cause destructors to be called for all completed base-class objects and 
  1840. member objects of the object being constructed before 
  1841. the exception is thrown. </indent>
  1842. <indent width=8 delay>*   If an array of objects has been partially constructed 
  1843. when an exception occurs, only the destructors for the 
  1844. constructed array elements will be called.</indent>
  1845. <indent width=8 delay>*   Exceptions thrown from destructors can be caught by 
  1846. enclosing the function that calls the destructor in a <b>try</b> 
  1847. block and provide a <b>catch</b> handler with the proper type.</indent>
  1848. <indent width=8 delay>*  A powerful reason for using inheritance with exceptions is for creating the ability to <b>catch</b> a variety of </indent>
  1849.  
  1850. </page>
  1851. <page>
  1852. <indent width=8 delay>*   related errors easily with concise notation. One could 
  1853. certainly <b>catch</b> each type of derived-class exception 
  1854. object individually, but it is much more concise to simply catch the base-class exception object. </indent>
  1855. <indent width=8 delay>*   The ANSI/ISO C++ draft standard specifies that 
  1856. when <b>new</b> fails, it <b>throw</b>s a <b>bad_alloc</b> exception 
  1857. (<b>bad_alloc</b> is defined in header file <b><new></b>). </indent>
  1858. <indent width=8 delay>*   Many compilers are not current with the ANSI/ISO 
  1859. C++ draft standard and still use the version of <b>new</b> that 
  1860. returns <b>0</b> on failure. </indent>
  1861. <indent width=8 delay>*  Function <b>set_new_handler</b> (prototyped in header 
  1862. file <b><new></b> or <b><new.h></b>) takes as its argument a function pointer for a function that takes no arguments and </indent>
  1863.  
  1864. </page>
  1865. <page>
  1866. <indent width=8 delay>*   returns <b>void</b>. The function pointer is registered as the 
  1867. function to call when <b>new</b> fails. Once a <b><i>new handler</i></b> is 
  1868. registered with <b>set_new_handler</b>, new will not throw 
  1869. <b>bad_alloc</b> on failure.</indent>
  1870. <indent width=8 delay>*   An object of class <b>auto_ptr</b> maintains a pointer to 
  1871. dynamically allocated memory. When an <b>auto_ptr</b> 
  1872. object goes out of scope, it automatically performs a 
  1873. <b>delete</b> operation on its pointer data member. Class template <b>auto_ptr</b> provides operators <b>*</b> and<b> -></b> so an 
  1874. <b>auto_ptr</b> object can be used like a regular pointer variable.</indent>
  1875. <indent width=8 delay>*  The C++ draft standard includes a hierarchy of 
  1876. exception classes headed by base class <b>exception</b> </indent>
  1877.  
  1878. </page>
  1879. <page>
  1880. <indent width=8 delay>*   (defined in header file <b><exception></b>) which offers the 
  1881. service <b>what()</b> that is overridden in each derived class 
  1882. to issue an appropriate error message.</indent>
  1883. <indent width=8 delay>*   By including <b>std::bad_exception </b>in the <b>throw</b> list of 
  1884. a function definition, if an unexpected exception 
  1885. occurs, <b>unexpected() </b>will throw <b>bad_exception</b> 
  1886. instead of terminating (by default) or instead of calling 
  1887. another function specified with <b>set_unexpected</b>.</indent>
  1888.  
  1889. </page>
  1890. <page>
  1891. <br>
  1892.  
  1893. </page>
  1894. <page>
  1895.  
  1896. </page>
  1897. </section>
  1898. <section type=Popup name=Debug title="Testing">
  1899. <page>
  1900. The programmer 
  1901. determines the order in 
  1902. which the exception 
  1903. handlers are listed. This 
  1904. order can affect how 
  1905. exceptions originating 
  1906. in that <b>try</b> block are 
  1907. handled. If you are 
  1908. getting unexpected 
  1909. behavior in your <br>
  1910.  
  1911. </page>
  1912. <page>
  1913. program's handling of 
  1914. exceptions it may be 
  1915. because an early <b>catch</b> 
  1916. block is intercepting 
  1917. and handling the 
  1918. exceptions before they 
  1919. reach your intended 
  1920. <b>catch</b> handler.<br>
  1921. <br>
  1922.  
  1923. </page>
  1924. <page>
  1925. Using inheritance with 
  1926. exceptions enables an 
  1927. exception handler to 
  1928. <b>catch</b> related errors 
  1929. with a rather concise 
  1930. notation. One could 
  1931. certainly <b>catch</b> each 
  1932. type of pointer or 
  1933. reference to a derived-
  1934. class exception object <br>
  1935.  
  1936. </page>
  1937. <page>
  1938. individually, but it is 
  1939. more concise to <b>catch</b> 
  1940. pointers or references 
  1941. to base-class exception 
  1942. objects instead. Also, 
  1943. catching pointers or 
  1944. references to derived-
  1945. class exception objects 
  1946. individually is subject 
  1947. to error if the <br>
  1948.  
  1949. </page>
  1950. <page>
  1951. programmer forgets to 
  1952. explicitly test for one or 
  1953. more of the derived-
  1954. class pointer or 
  1955. reference types.<br>
  1956. <br>
  1957.  
  1958. </page>
  1959. <page>
  1960. To <b>catch</b> all exceptions 
  1961. that might be thrown in 
  1962. a <b>try</b> block, use <tt><i><b>catch( 
  1963. ... )</b></i></tt>.<br>
  1964. <br>
  1965.  
  1966. </page>
  1967. </section>
  1968. <section type=Popup name=Terminology title="Terminology">
  1969. <page>
  1970. <font size=14>
  1971. A<br>
  1972. <b>abort</b> 
  1973. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  1974. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  1975. <a href="%s7p3"><img src=iicons/bullbib.gif></a>
  1976. <a href="%s10p1"><img src=iicons/bullbib.gif></a>
  1977.  
  1978. <a href="%s10p2"><img src=iicons/bullbib.gif></a>
  1979. <a href="%s14p8"><img src=iicons/bullbib.gif></a>
  1980. <a href="%s14p8"><img src=iicons/bullbib.gif></a>
  1981. <br>
  1982. <b>assert</b> macro 
  1983. <a href="%s14p0"><img src=iicons/bullbib.gif></a>
  1984. <br>
  1985. <b>auto_ptr</b> 
  1986. <a href="%s15p0"><img src=iicons/bullbib.gif></a>
  1987. <a href="%s15p1"><img src=iicons/bullbib.gif></a>
  1988. <br>
  1989. B<br>
  1990. <b>bad_alloc</b> exception 
  1991.  
  1992. <a href="%s14p0"><img src=iicons/bullbib.gif></a>
  1993. <a href="%s14p4"><img src=iicons/bullbib.gif></a>
  1994. <a href="%s14p8"><img src=iicons/bullbib.gif></a>
  1995. <a href="%s16p1"><img src=iicons/bullbib.gif></a>
  1996. <br>
  1997. <b>bad_cast</b> 
  1998. <a href="%s16p1"><img src=iicons/bullbib.gif></a>
  1999. <br>
  2000. <b>bad_typeid</b> 
  2001. <a href="%s16p1"><img src=iicons/bullbib.gif></a>
  2002. <br>
  2003. C<br>
  2004. <b>catch</b> all exceptions 
  2005. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  2006.  
  2007. <a href="^Errors::c:s0p19"><img src=iicons/bullbib.gif></a>
  2008. <br>
  2009. <b>catch</b> an exception 
  2010. <a href="%s12p1"><img src=iicons/bullbib.gif></a>
  2011. <br>
  2012. </font>
  2013.  
  2014. </page>
  2015. <page>
  2016. <font size=14>
  2017. <b>catch</b> block 
  2018. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  2019. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  2020. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  2021.  
  2022. <a href="%s7p3"><img src=iicons/bullbib.gif></a>
  2023. <br>
  2024. <b>catch( ... )</b> 
  2025. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  2026. <a href="%s7p4"><img src=iicons/bullbib.gif></a>
  2027. <a href="%s7p6"><img src=iicons/bullbib.gif></a>
  2028. <a href="^Engineer::c:s0p14"><img src=iicons/bullbib.gif></a>
  2029.  
  2030. <a href="^Engineer::c:s0p17"><img src=iicons/bullbib.gif></a>
  2031. <a href="^Debug::c:s0p5"><img src=iicons/bullbib.gif></a>
  2032. <br>
  2033. D<br>
  2034. <b>dynamic_cast</b> 
  2035. <a href="%s16p1"><img src=iicons/bullbib.gif></a>
  2036. <br>
  2037. E<br>
  2038. empty exception 
  2039. specification 
  2040. <a href="%s9p1"><img src=iicons/bullbib.gif></a>
  2041. <br>
  2042. empty <b>throw</b> statement 
  2043.  
  2044. <a href="^Errors::c:s0p17"><img src=iicons/bullbib.gif></a>
  2045. <br>
  2046. enclosing <b>try</b> block 
  2047. <a href="%s7p3"><img src=iicons/bullbib.gif></a>
  2048. <br>
  2049. <b>exception</b> 
  2050. <a href="%s16p0"><img src=iicons/bullbib.gif></a>
  2051. <br>
  2052. </font>
  2053.  
  2054. </page>
  2055. <page>
  2056. <font size=14>
  2057. exception handler 
  2058. <a href="%s1p5"><img src=iicons/bullbib.gif></a>
  2059. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  2060.  
  2061. <a href="%s7p1"><img src=iicons/bullbib.gif></a>
  2062. <br>
  2063. exception object 
  2064. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  2065. <br>
  2066. exception specification 
  2067.  
  2068. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  2069. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  2070. <br>
  2071. <b>exit</b> 
  2072. <a href="%s3p2"><img src=iicons/bullbib.gif></a>
  2073. <a href="%s14p8"><img src=iicons/bullbib.gif></a>
  2074. <br>
  2075. F<br>
  2076. fault tolerance 
  2077. <a href="%s1p7"><img src=iicons/bullbib.gif></a>
  2078. <br>
  2079. H<br>
  2080. handler 
  2081. <a href="^Engineer::c:s0p10"><img src=iicons/bullbib.gif></a>
  2082. <br>
  2083. I<br>
  2084. <b>invalid_argument</b> 
  2085. <a href="%s16p2"><img src=iicons/bullbib.gif></a>
  2086. <br>
  2087. L<br>
  2088. <b>length_error</b> 
  2089. <a href="%s16p2"><img src=iicons/bullbib.gif></a>
  2090. <br>
  2091. </font>
  2092.  
  2093. </page>
  2094. <page>
  2095. <font size=14>
  2096. <b>logic_error</b> 
  2097. <a href="%s16p0"><img src=iicons/bullbib.gif></a>
  2098. <br>
  2099. M<br>
  2100. <b><memory></b> 
  2101. <a href="%s15p0"><img src=iicons/bullbib.gif></a>
  2102. <br>
  2103. mission critical 
  2104. <a href="%s3p0"><img src=iicons/bullbib.gif></a>
  2105. <br>
  2106. N<br>
  2107. nested scope 
  2108. <a href="%s6p2"><img src=iicons/bullbib.gif></a>
  2109. <br>
  2110. <b><new></b> 
  2111. <a href="%s14p0"><img src=iicons/bullbib.gif></a>
  2112. <br>
  2113. <b>new_handler</b> 
  2114. <a href="%s3p3"><img src=iicons/bullbib.gif></a>
  2115. <br>
  2116. <b>nothrow</b> 
  2117. <a href="%s14p5"><img src=iicons/bullbib.gif></a>
  2118. <br>
  2119. O<br>
  2120. <b>out_of_range</b> 
  2121. <a href="%s16p2"><img src=iicons/bullbib.gif></a>
  2122. <br>
  2123. <b>overflow_error</b> 
  2124. <a href="%s16p3"><img src=iicons/bullbib.gif></a>
  2125. <br>
  2126. R<br>
  2127. </font>
  2128.  
  2129. </page>
  2130. <page>
  2131. <font size=14>
  2132. rethrow an exception 
  2133. <a href="%s7p9"><img src=iicons/bullbib.gif></a>
  2134.  
  2135. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  2136. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  2137. <br>
  2138. <b>runtime_error</b> 
  2139.  
  2140. <a href="%s16p0"><img src=iicons/bullbib.gif></a>
  2141. <a href="%s16p2"><img src=iicons/bullbib.gif></a>
  2142. <br>
  2143. S<br>
  2144. <b>set_new_handler</b> 
  2145. <a href="%s3p3"><img src=iicons/bullbib.gif></a>
  2146.  
  2147. <a href="%s14p6"><img src=iicons/bullbib.gif></a>
  2148. <a href="%s14p8"><img src=iicons/bullbib.gif></a>
  2149. <br>
  2150. <b>set_terminate</b> 
  2151. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  2152. <a href="%s10p1"><img src=iicons/bullbib.gif></a>
  2153. <br>
  2154. <b>set_unexpected</b> 
  2155. <a href="%s9p2"><img src=iicons/bullbib.gif></a>
  2156.  
  2157. <a href="%s10p0"><img src=iicons/bullbib.gif></a>
  2158. <a href="%s16p1"><img src=iicons/bullbib.gif></a>
  2159. <br>
  2160. stack unwinding 
  2161. <a href="^Errors::c:s0p0"><img src=iicons/bullbib.gif></a>
  2162. <a href="%s3p3"><img src=iicons/bullbib.gif></a>
  2163.  
  2164. <a href="%s10p0"><img src=iicons/bullbib.gif></a>
  2165. <a href="%s11p0"><img src=iicons/bullbib.gif></a>
  2166. <a href="%s12p2"><img src=iicons/bullbib.gif></a>
  2167. <br>
  2168. <b>std::bad_exception</b> 
  2169. <a href="%s16p1"><img src=iicons/bullbib.gif></a>
  2170. <br>
  2171. <b><stdexcept></b> 
  2172. <a href="%s16p0"><img src=iicons/bullbib.gif></a>
  2173. <br>
  2174. </font>
  2175.  
  2176. </page>
  2177. <page>
  2178. <font size=14>
  2179. T<br>
  2180. <b>terminate</b> 
  2181. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  2182. <a href="^Errors::c:s0p6"><img src=iicons/bullbib.gif></a>
  2183. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  2184. <a href="%s7p3"><img src=iicons/bullbib.gif></a>
  2185.  
  2186. <a href="%s7p9"><img src=iicons/bullbib.gif></a>
  2187. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  2188. <a href="%s10p0"><img src=iicons/bullbib.gif></a>
  2189. <br>
  2190. <b>throw</b> an exception 
  2191. <a href="%s7p11"><img src=iicons/bullbib.gif></a>
  2192. <br>
  2193. <b>throw</b> expression 
  2194. <a href="%s9p2"><img src=iicons/bullbib.gif></a>
  2195. <br>
  2196. throw list 
  2197. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  2198. <br>
  2199. throw point 
  2200. <a href="%s4p3"><img src=iicons/bullbib.gif></a>
  2201. <br>
  2202. <b>throw</b> with no 
  2203. arguments 
  2204. <a href="%s8p0"><img src=iicons/bullbib.gif></a>
  2205. <br>
  2206. <b>throw()</b> 
  2207. <a href="%s9p1"><img src=iicons/bullbib.gif></a>
  2208. <br>
  2209. <b>try</b> block 
  2210. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  2211. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  2212. <a href="%s5p4"><img src=iicons/bullbib.gif></a>
  2213. <a href="%s6p2"><img src=iicons/bullbib.gif></a>
  2214.  
  2215. <a href="%s7p3"><img src=iicons/bullbib.gif></a>
  2216. <a href="%s7p7"><img src=iicons/bullbib.gif></a>
  2217. <a href="%s7p10"><img src=iicons/bullbib.gif></a>
  2218. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  2219. <a href="%s11p2"><img src=iicons/bullbib.gif></a>
  2220. <a href="%s12p2"><img src=iicons/bullbib.gif></a>
  2221.  
  2222. <a href="%s14p3"><img src=iicons/bullbib.gif></a>
  2223. <br>
  2224. U<br>
  2225. </font>
  2226.  
  2227. </page>
  2228. <page>
  2229. <font size=14>
  2230. <b>under flow_error</b> 
  2231. <a href="%s16p3"><img src=iicons/bullbib.gif></a>
  2232. <br>
  2233. <b>unexpected()</b> 
  2234. <a href="%s16p1"><img src=iicons/bullbib.gif></a>
  2235. <br>
  2236. <br>
  2237. </font>
  2238.  
  2239. </page>
  2240. </section>
  2241. <section type=Popup name=Illustration title="Illustrations">
  2242. <page>
  2243. <a href="^Code::c:s0p0">Fig. 13.1</a>  A simple exception-handling example with divide by zero.<br>
  2244. <a href="^Code::c:s0p1">Fig. 13.2</a>  Rethrowing an exception.<br>
  2245. <a href="^Code::c:s0p2">Fig. 13.3</a>  Demonstration of stack unwinding.<br>
  2246. <a href="^Code::c:s0p3">Fig. 13.4</a>  Demonstrating <b>new</b> returning <b>0</b> on failure.<br>
  2247. <a href="^Code::c:s0p4">Fig. 13.5</a>  Demonstrating <b>new</b> throwing <b>bad_alloc</b> on failure.<br>
  2248. <a href="^Code::c:s0p5">Fig. 13.6</a>  Demonstrating <b>set_new_handler</b>.<br>
  2249. <a href="^Code::c:s0p6">Fig. 13.7</a>  Demonstrating <b>auto_ptr</b>.<br>
  2250. <br>
  2251.  
  2252. </page>
  2253. </section>
  2254. <section type=Popup name=Exercises title="Exercises">
  2255. <page pagename="Exercise 13.1">
  2256. <b>Exercise 13.1</b><br>
  2257. List five common examples of exceptions.<br>
  2258. <foreign  name="answers" url="^Answers::c:s0p0">
  2259. <br>
  2260. <br>
  2261.  
  2262. </page>
  2263. <page pagename="Exercise 13.2">
  2264. <b>Exercise 13.2</b><br>
  2265. Give several reasons why exception-handling techniques should not be used for 
  2266. conventional program control.<br>
  2267. <foreign  name="answers" url="^Answers::c:s0p1">
  2268. <br>
  2269. <br>
  2270.  
  2271. </page>
  2272. <page pagename="Exercise 13.3">
  2273. <b>Exercise 13.3</b><br>
  2274. Why are exceptions appropriate for dealing with errors produced by library 
  2275. functions? <br>
  2276. <foreign  name="answers" url="^Answers::c:s0p2">
  2277. <br>
  2278. <br>
  2279.  
  2280. </page>
  2281. <page pagename="Exercise 13.4">
  2282. <b>Exercise 13.4</b><br>
  2283. What is a "resource leak?"<br>
  2284. <foreign  name="answers" url="^Answers::c:s0p3">
  2285. <br>
  2286. <br>
  2287.  
  2288. </page>
  2289. <page pagename="Exercise 13.5">
  2290. <b>Exercise 13.5</b><br>
  2291. If no exceptions are thrown in a <b>try</b> block, where does control proceed to after 
  2292. the <b>try</b> block completes execution?<br>
  2293. <foreign  name="answers" url="^Answers::c:s0p4">
  2294. <br>
  2295. <br>
  2296.  
  2297. </page>
  2298. <page pagename="Exercise 13.6">
  2299. <b>Exercise 13.6</b><br>
  2300. What happens if an exception is thrown outside a <b>try</b> block?<br>
  2301. <foreign  name="answers" url="^Answers::c:s0p5">
  2302. <br>
  2303. <br>
  2304.  
  2305. </page>
  2306. <page pagename="Exercise 13.7">
  2307. <b>Exercise 13.7</b><br>
  2308. Give a key advantage and a key disadvantage of using <b>catch(...)</b>.<br>
  2309. <foreign  name="answers" url="^Answers::c:s0p6">
  2310. <br>
  2311. <br>
  2312.  
  2313. </page>
  2314. <page pagename="Exercise 13.8">
  2315. <b>Exercise 13.8</b><br>
  2316. What happens if no <b>catch</b> handler matches the type of a thrown object?<br>
  2317. <foreign  name="answers" url="^Answers::c:s0p7">
  2318. <br>
  2319. <br>
  2320.  
  2321. </page>
  2322. <page pagename="Exercise 13.9">
  2323. <b>Exercise 13.9</b><br>
  2324. What happens if several handlers match the type of the thrown object?<br>
  2325. <foreign  name="answers" url="^Answers::c:s0p8">
  2326. <br>
  2327. <br>
  2328.  
  2329. </page>
  2330. <page pagename="Exercise 13.10">
  2331. <b>Exercise 13.10</b><br>
  2332. Why would a programmer specify a base-class type as the type of a <b>catch</b> 
  2333. handler and then <b>throw</b> objects of derived-class types?<br>
  2334. <foreign  name="answers" url="^Answers::c:s0p9">
  2335. <br>
  2336. <br>
  2337.  
  2338. </page>
  2339. <page pagename="Exercise 13.11">
  2340. <b>Exercise 13.11</b><br>
  2341. How might a <b>catch</b> handler be written to process related types of errors without 
  2342. using inheritance among exception classes?<br>
  2343. <foreign  name="answers" url="^Answers::c:s0p10">
  2344. <br>
  2345. <br>
  2346.  
  2347. </page>
  2348. <page pagename="Exercise 13.12">
  2349. <b>Exercise 13.12</b><br>
  2350. What pointer type is used in a <b>catch</b> handler to catch any exception of any 
  2351. pointer type?<br>
  2352. <foreign  name="answers" url="^Answers::c:s0p11">
  2353. <br>
  2354. <br>
  2355.  
  2356. </page>
  2357. <page pagename="Exercise 13.13">
  2358. <b>Exercise 13.13</b><br>
  2359. Suppose a <b>catch</b> handler with a precise match to an exception object type is 
  2360. available. Under what circumstances might a different handler be executed for 
  2361. exception objects of that type?<br>
  2362. <foreign  name="answers" url="^Answers::c:s0p12">
  2363. <br>
  2364. <br>
  2365.  
  2366. </page>
  2367. <page pagename="Exercise 13.14">
  2368. <b>Exercise 13.14</b><br>
  2369. Must throwing an exception cause program termination?<br>
  2370. <foreign  name="answers" url="^Answers::c:s0p13">
  2371. <br>
  2372. <br>
  2373.  
  2374. </page>
  2375. <page pagename="Exercise 13.15">
  2376. <b>Exercise 13.15</b><br>
  2377. What happens when a <b>catch</b> handler <b>throw</b>s an exception?<br>
  2378. <foreign  name="answers" url="^Answers::c:s0p14">
  2379. <br>
  2380. <br>
  2381.  
  2382. </page>
  2383. <page pagename="Exercise 13.16">
  2384. <b>Exercise 13.16</b><br>
  2385. What does the statement <b>throw;</b> do?<br>
  2386. <foreign  name="answers" url="^Answers::c:s0p15">
  2387. <br>
  2388. <br>
  2389.  
  2390. </page>
  2391. <page pagename="Exercise 13.17">
  2392. <b>Exercise 13.17</b><br>
  2393. How does the programmer restrict the exception types that can be thrown from a 
  2394. function?<br>
  2395. <foreign  name="answers" url="^Answers::c:s0p16">
  2396. <br>
  2397. <br>
  2398.  
  2399. </page>
  2400. <page pagename="Exercise 13.18">
  2401. <b>Exercise 13.18</b><br>
  2402. What happens if a function does <b>throw</b> an exception of a type not allowed by the 
  2403. exception specification for the function?<br>
  2404. <foreign  name="answers" url="^Answers::c:s0p17">
  2405. <br>
  2406. <br>
  2407.  
  2408. </page>
  2409. <page pagename="Exercise 13.19">
  2410. <b>Exercise 13.19</b><br>
  2411. What happens to the automatic objects that have been constructed in a <b>try</b> block 
  2412. when that block <b>throw</b>s an exception?<br>
  2413. <foreign  name="answers" url="^Answers::c:s0p18">
  2414. <br>
  2415. <br>
  2416.  
  2417. </page>
  2418. <page pagename="Exercise 13.20">
  2419. <b>Exercise 13.20</b><br>
  2420. List the various exceptional conditions that have occurred in programs 
  2421. throughout this text. List as many additional exceptional conditions as you can. 
  2422. For each of these, describe briefly how a program would typically handle the 
  2423. exception using the exception-handling techniques discussed in this chapter. 
  2424. Some typical exceptions are: Division by zero, arithmetic overflow, array 
  2425. subscript out of bounds, exhaustion of the free store, etc.<spacer width=20 height=1><br>
  2426. <br>
  2427. <br>
  2428.  
  2429. </page>
  2430. <page pagename="Exercise 13.21">
  2431. <b>Exercise 13.21</b><br>
  2432. Under what circumstances would the programmer not provide a parameter name 
  2433. when defining the type of the object that will be caught by a handler?<br>
  2434. <foreign  name="answers" url="^Answers::c:s0p19">
  2435. <br>
  2436.  
  2437. </page>
  2438. <page pagename="Exercise 13.22">
  2439. <b>Exercise 13.22</b><br>
  2440. A program contains the statement <br>
  2441. <font size=2><br></font><font size=11><pre>
  2442. throw;<p>
  2443. </pre></font>
  2444. Where would you normally expect to find such a statement? What if that 
  2445. statement appeared in a different part of a program?<br>
  2446. <br>
  2447.  
  2448. </page>
  2449. <page pagename="Exercise 13.23">
  2450. <b>Exercise 13.23</b><br>
  2451. Under what circumstances would you use the following statement?<br>
  2452. <font size=2><br></font><font size=11><pre>
  2453. catch(...) { throw; }<p>
  2454. </pre></font>
  2455. <br>
  2456. <br>
  2457.  
  2458. </page>
  2459. <page pagename="Exercise 13.24">
  2460. <b>Exercise 13.24</b><br>
  2461. Compare and contrast exception handling with the various other error-
  2462. processing schemes discussed in the text.<br>
  2463. <br>
  2464. <br>
  2465.  
  2466. </page>
  2467. <page pagename="Exercise 13.25">
  2468. <b>Exercise 13.25</b><br>
  2469. List the benefits of exception handling over conventional means of error 
  2470. processing.<br>
  2471. <br>
  2472. <br>
  2473.  
  2474. </page>
  2475. <page pagename="Exercise 13.26">
  2476. <b>Exercise 13.26</b><br>
  2477. Provide reasons why exceptions should not be used as an alternate form of 
  2478. program control.<br>
  2479. <br>
  2480. <br>
  2481.  
  2482. </page>
  2483. <page pagename="Exercise 13.27">
  2484. <b>Exercise 13.27</b><br>
  2485. Describe a technique for handling related exceptions.<br>
  2486. <foreign  name="answers" url="^Answers::c:s0p20">
  2487. <br>
  2488.  
  2489. </page>
  2490. <page pagename="Exercise 13.28">
  2491. <b>Exercise 13.28</b><br>
  2492. Until this chapter, we have found that dealing with errors detected by 
  2493. constructors is a bit awkward. Exception handling gives us a much better means 
  2494. of dealing with such errors. Consider a constructor for a <b>String</b> class. The 
  2495. constructor uses <b>new</b> to obtain space from the free store. Suppose <b>new</b> fails. 
  2496. Show how you would deal with this without exception handling. Discuss the key 
  2497. issues. Show how you would deal with such memory exhaustion with exception 
  2498. handling. Explain why the exception handling method is superior.<br>
  2499. <br>
  2500. <br>
  2501.  
  2502. </page>
  2503. <page pagename="Exercise 13.29">
  2504. <b>Exercise 13.29</b><br>
  2505. Suppose a program <b>throw</b>s an exception and the appropriate exception handler 
  2506. begins executing. Now suppose that the exception handler itself <b>throw</b>s the 
  2507. same exception. Does this create an infinite recursion? Write a C++ program to 
  2508. check your observation.<br>
  2509. <br>
  2510. <foreign  name="answers" url="^Answers::c:s0p21">
  2511. <br>
  2512.  
  2513. </page>
  2514. <page pagename="Exercise 13.30">
  2515. <b>Exercise 13.30</b><br>
  2516. Use inheritance to create a base exception class and various derived exception 
  2517. classes. Then show that a <b>catch</b> handler specifying the base class can <b>catch</b> 
  2518. derived-class exceptions.<br>
  2519. <br>
  2520. <br>
  2521.  
  2522. </page>
  2523. <page pagename="Exercise 13.31">
  2524. <b>Exercise 13.31</b><br>
  2525. Show a conditional expression that returns either a <b>double</b> or an <b>int</b>. Provide an 
  2526. <b>int</b> <b>catch</b> handler and a <b>double</b> <b>catch</b> handler. Show that only the <b>double</b> <b>catch</b> 
  2527. handler executes regardless of whether the <b>int</b> or the <b>double</b> is returned.<br>
  2528. <br>
  2529. <foreign  name="answers" url="^Answers::c:s0p22">
  2530. <br>
  2531.  
  2532. </page>
  2533. <page pagename="Exercise 13.32">
  2534. <b>Exercise 13.32</b><br>
  2535. Write a C++ program designed to generate and handle a memory exhaustion 
  2536. error. Your program should loop on a request to create dynamic storage through 
  2537. operator <b>new</b>.<br>
  2538. <br>
  2539. <br>
  2540.  
  2541. </page>
  2542. <page pagename="Exercise 13.33">
  2543. <b>Exercise 13.33</b><br>
  2544. Write a C++ program which shows that all destructors for objects constructed in 
  2545. a block are called before an exception is thrown from that block.<br>
  2546. <br>
  2547. <br>
  2548.  
  2549. </page>
  2550. <page pagename="Exercise 13.34">
  2551. <b>Exercise 13.34</b><br>
  2552. Write a C++ program which shows that member object destructors are called for 
  2553. only those member objects that were constructed before an exception occurred.<br>
  2554. <br>
  2555. <foreign  name="answers" url="^Answers::c:s0p23">
  2556. <br>
  2557.  
  2558. </page>
  2559. <page pagename="Exercise 13.35">
  2560. <b>Exercise 13.35</b><br>
  2561. Write a C++ program that demonstrates how any exception is caught with 
  2562. <b>catch(...)</b>.<br>
  2563. <br>
  2564. <br>
  2565.  
  2566. </page>
  2567. <page pagename="Exercise 13.36">
  2568. <b>Exercise 13.36</b><br>
  2569. Write a C++ program which shows that the order of exception handlers is 
  2570. important. The first matching handler is the one that executes. Compile and run 
  2571. your program two different ways to show that two different handlers execute 
  2572. with two different effects.<br>
  2573. <br>
  2574. <br>
  2575.  
  2576. </page>
  2577. <page pagename="Exercise 13.37">
  2578. <b>Exercise 13.37</b><br>
  2579. Write a C++ program that shows a constructor passing information about 
  2580. constructor failure to an exception handler after a <b>try</b> block.<br>
  2581. <br>
  2582. <br>
  2583.  
  2584. </page>
  2585. <page pagename="Exercise 13.38">
  2586. <b>Exercise 13.38</b><br>
  2587. Write a C++ program that uses a multiple inheritance hierarchy of exception 
  2588. classes to create a situation in which the order of exception handlers matters.<br>
  2589. <br>
  2590. <br>
  2591.  
  2592. </page>
  2593. <page pagename="Exercise 13.39">
  2594. <b>Exercise 13.39</b><br>
  2595. Using <b>setjmp</b>/<b>longjmp</b>, a program can transfer control immediately to an error 
  2596. routine from a deeply nested function invocation. Unfortunately, as the stack is 
  2597. unwound, destructors are not called for the automatic objects that were created 
  2598. during the sequence of nested function calls. Write a C++ program which 
  2599. demonstrates that these destructors are, in fact, not called.<br>
  2600. <br>
  2601. <br>
  2602.  
  2603. </page>
  2604. <page pagename="Exercise 13.40">
  2605. <b>Exercise 13.40</b><br>
  2606. Write a C++ program that illustrates rethrowing an exception.<br>
  2607. <br>
  2608. <foreign  name="answers" url="^Answers::c:s0p24">
  2609. <br>
  2610.  
  2611. </page>
  2612. <page pagename="Exercise 13.41">
  2613. <b>Exercise 13.41</b><br>
  2614. Write a C++ program that uses <b>set_unexpected</b> to set a user-defined function 
  2615. for <b>unexpected</b>, uses <b>set_unexpected</b> again, and then resets <b>unexpected</b> back to 
  2616. its previous function. Write a similar program to test <b>set_terminate</b> and 
  2617. <b>terminate</b>.<br>
  2618. <br>
  2619. <br>
  2620.  
  2621. </page>
  2622. <page pagename="Exercise 13.42">
  2623. <b>Exercise 13.42</b><br>
  2624. Write a C++ program which shows that a function with its own <b>try</b> block does 
  2625. not have to catch every possible error generated within the <b>try</b>. Some exceptions 
  2626. can slip through to, and be handled in, outer scopes. <br>
  2627. <br>
  2628. <foreign  name="answers" url="^Answers::c:s0p25">
  2629. <br>
  2630.  
  2631. </page>
  2632. <page pagename="Exercise 13.43">
  2633. <b>Exercise 13.43</b><br>
  2634. Write a C++ program that <b>throw</b>s an error from a deeply nested function call and 
  2635. still has the <b>catch</b> handler following the <b>try</b> block enclosing the call chain catch 
  2636. the exception.<br>
  2637. <br>
  2638. <br>
  2639.  
  2640. </page>
  2641. </section>
  2642. <section type=Popup name=Objective title="Objectives">
  2643. <page>
  2644. <indent width=8 delay>*   To use <b>try</b>, <b>throw</b> and <b>catch</b> to watch for, indicate and handle exceptions, 
  2645. respectively.</indent>
  2646. <indent width=8 delay>*   To process uncaught and unexpected exceptions.</indent>
  2647. <indent width=8 delay>*   To be able to process <b>new</b> failures.</indent>
  2648. <indent width=8 delay>*   To use <b>auto_ptr</b> to prevent memory leaks.</indent>
  2649. <foreign  name="audio" url="~audio/Ch13/13obj.au">
  2650.  
  2651. </page>
  2652. <page>
  2653. <indent width=8 delay>*   To understand the standard exception hierarchy.  </indent>
  2654.  
  2655. </page>
  2656. </section>
  2657. <section type=Popup name=Practice title="Good Practices">
  2658. <page>
  2659. Avoid using exception 
  2660. handling for purposes 
  2661. other than error 
  2662. handling because this 
  2663. can reduce program 
  2664. clarity.<br>
  2665. <br>
  2666.  
  2667. </page>
  2668. <page>
  2669. Use exceptions for 
  2670. errors that must be 
  2671. processed in a different 
  2672. scope from where they 
  2673. occur. Use other means 
  2674. of error handling for 
  2675. errors that will be 
  2676. processed in the scope 
  2677. in which they occur.<br>
  2678. <br>
  2679.  
  2680. </page>
  2681. <page>
  2682. Use conventional error-
  2683. handling techniques 
  2684. rather than exception 
  2685. handling for 
  2686. straightforward, local 
  2687. error processing in 
  2688. which a program is 
  2689. easily able to deal with 
  2690. its own errors. <br>
  2691. <br>
  2692.  
  2693. </page>
  2694. <page>
  2695. Associating each type 
  2696. of execution-time error 
  2697. with an appropriately 
  2698. named exception object 
  2699. improves program 
  2700. clarity.<br>
  2701. <br>
  2702.  
  2703. </page>
  2704. </section>
  2705. <section type=Popup name=Perform title="Performance">
  2706. <page>
  2707. Exception handling is 
  2708. generally implemented 
  2709. in compilers in such a 
  2710. manner that when an 
  2711. exception does not 
  2712. occur, little or no 
  2713. overhead is imposed by 
  2714. the presence of 
  2715. exception-handling 
  2716. code. When exceptions <br>
  2717.  
  2718. </page>
  2719. <page>
  2720. happen, they do incur 
  2721. execution-time 
  2722. overhead. Certainly the 
  2723. presence of exception-
  2724. handling code makes 
  2725. the program consume 
  2726. more memory.<br>
  2727. <br>
  2728.  
  2729. </page>
  2730. <page>
  2731. Although it is possible 
  2732. to use exception 
  2733. handling for purposes 
  2734. other than error 
  2735. handling, this can 
  2736. reduce program 
  2737. performance.<br>
  2738. <br>
  2739.  
  2740. </page>
  2741. </section>
  2742. <section type=Popup name=Code title="Code Examples">
  2743. <page>
  2744. <font size=18>Figure 13.1  A simple exception-handling example with divide by zero.</font><br>
  2745. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2746. <param  name="file" value="code/ch13/fig13_01.txt">
  2747. </applet><br>
  2748. <br>
  2749. <foreign  name="copy2disk" url="!jcpy Source/fig13_01.jar">
  2750. <foreign  name="audio" url="~audio/Ch13/13fig001.au">
  2751. <foreign  name="execute" url="!jarexe Run/fig13_01.jar">
  2752. <br>
  2753.  
  2754. </page>
  2755. <page>
  2756. <font size=18>Figure 13.2  Rethrowing an exception.</font><br>
  2757. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2758. <param  name="file" value="code/ch13/fig13_02.txt">
  2759. </applet><br>
  2760. <br>
  2761. <foreign  name="copy2disk" url="!jcpy Source/fig13_02.jar">
  2762. <foreign  name="audio" url="~audio/Ch13/13fig002.au">
  2763. <foreign  name="execute" url="!jarexe Run/fig13_02.jar">
  2764. <br>
  2765.  
  2766. </page>
  2767. <page>
  2768. <font size=18>Figure 13.3  Demonstration of stack unwinding.</font><br>
  2769. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2770. <param  name="file" value="code/ch13/fig13_03.txt">
  2771. </applet><br>
  2772. <br>
  2773. <foreign  name="copy2disk" url="!jcpy Source/fig13_03.jar">
  2774. <foreign  name="audio" url="~audio/Ch13/13fig003.au">
  2775. <foreign  name="execute" url="!jarexe Run/fig13_03.jar">
  2776. <br>
  2777.  
  2778. </page>
  2779. <page>
  2780. <font size=18>Figure 13.4  Demonstrating <b>new</b> returning <b>0</b> on failure.</font><br>
  2781. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2782. <param  name="file" value="code/ch13/fig13_04.txt">
  2783. </applet><br>
  2784. <br>
  2785. <foreign  name="copy2disk" url="!jcpy Source/fig13_04.jar">
  2786. <foreign  name="audio" url="~audio/Ch13/13fig004.au">
  2787. <foreign  name="execute" url="!jarexe Run/fig13_04.jar">
  2788. <br>
  2789.  
  2790. </page>
  2791. <page>
  2792. <font size=18>Figure 13.5  Demonstrating <b>new</b> throwing <b>bad_alloc</b> on failure.</font><br>
  2793. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2794. <param  name="file" value="code/ch13/fig13_05.txt">
  2795. </applet><br>
  2796. <br>
  2797. <foreign  name="copy2disk" url="!jcpy Source/fig13_05.jar">
  2798. <foreign  name="audio" url="~audio/Ch13/13fig005.au">
  2799. <foreign  name="execute" url="!jarexe Run/fig13_05.jar">
  2800. <br>
  2801.  
  2802. </page>
  2803. <page>
  2804. <font size=18>Figure 13.6  Demonstrating set_new_handler.</font><br>
  2805. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2806. <param  name="file" value="code/ch13/fig13_06.txt">
  2807. </applet><br>
  2808. <br>
  2809. <foreign  name="copy2disk" url="!jcpy Source/fig13_06.jar">
  2810. <foreign  name="audio" url="~audio/Ch13/13fig006.au">
  2811. <foreign  name="execute" url="!jarexe Run/fig13_06.jar">
  2812. <br>
  2813.  
  2814. </page>
  2815. <page>
  2816. <font size=18>Figure 13.7  Demonstrating auto_ptr.</font><br>
  2817. <applet code=gsl.win.helper.TextBox width=580 height=300>
  2818. <param  name="file" value="code/ch13/fig13_07.txt">
  2819. </applet><br>
  2820. <br>
  2821. <foreign  name="copy2disk" url="!jcpy Source/fig13_07.jar">
  2822. <foreign  name="audio" url="~audio/Ch13/13fig007.au">
  2823. <foreign  name="execute" url="!jarexe Run/fig13_07.jar">
  2824. <br>
  2825.  
  2826. </page>
  2827. </section>
  2828. <section type=Popup name=Engineer title="Engineering">
  2829. <page>
  2830. Flow of control with 
  2831. conventional control 
  2832. structures is generally 
  2833. clearer and more 
  2834. efficient than with 
  2835. exceptions.<br>
  2836. <br>
  2837.  
  2838. </page>
  2839. <page>
  2840. Exception handling is 
  2841. well suited to systems 
  2842. of separately developed 
  2843. components. Exception 
  2844. handling makes it 
  2845. easier to combine the 
  2846. components. Each 
  2847. component can perform 
  2848. its own exception 
  2849. detection separate from <br>
  2850.  
  2851. </page>
  2852. <page>
  2853. the handling of the 
  2854. exceptions in another 
  2855. scope.<br>
  2856. <br>
  2857.  
  2858. </page>
  2859. <page>
  2860. When dealing with 
  2861. libraries, the caller of 
  2862. the library function will 
  2863. likely have unique error 
  2864. processing in mind for 
  2865. an exception generated 
  2866. in the library function. 
  2867. It is unlikely that a 
  2868. library function will 
  2869. perform error <br>
  2870.  
  2871. </page>
  2872. <page>
  2873. processing that would 
  2874. meet the unique needs 
  2875. of all users. Therefore, 
  2876. exceptions are an 
  2877. appropriate means for 
  2878. dealing with errors 
  2879. produced by library 
  2880. functions.<br>
  2881. <br>
  2882.  
  2883. </page>
  2884. <page>
  2885. A key to exception 
  2886. handling is that the 
  2887. portion of a program or 
  2888. system that will handle 
  2889. the exception can be 
  2890. quite different or 
  2891. distant from the portion 
  2892. of the program that 
  2893. detected and generated <br>
  2894.  
  2895. </page>
  2896. <page>
  2897. the exceptional 
  2898. situation.<br>
  2899. <br>
  2900.  
  2901. </page>
  2902. <page>
  2903. If it is necessary to pass 
  2904. information about the 
  2905. error that caused an 
  2906. exception, such 
  2907. information can be 
  2908. placed in the thrown 
  2909. object. The <b>catch</b> 
  2910. handler would then 
  2911. contain a parameter 
  2912. name through which <br>
  2913.  
  2914. </page>
  2915. <page>
  2916. that information could 
  2917. be referenced.<br>
  2918. <br>
  2919.  
  2920. </page>
  2921. <page>
  2922. An object can be 
  2923. thrown without 
  2924. containing information 
  2925. to be passed; in this 
  2926. case, mere knowledge 
  2927. that an exception of this 
  2928. type has been thrown 
  2929. may provide sufficient 
  2930. information for the <br>
  2931.  
  2932. </page>
  2933. <page>
  2934. handler to do its job 
  2935. correctly.<br>
  2936. <br>
  2937.  
  2938. </page>
  2939. <page>
  2940. A weakness with 
  2941. catching exceptions 
  2942. with <b>catch(...)</b> is that 
  2943. you normally cannot be 
  2944. sure what the exception 
  2945. type is. Another 
  2946. weakness is that 
  2947. without a named 
  2948. parameter, there is no 
  2949. way to refer to the <br>
  2950.  
  2951. </page>
  2952. <page>
  2953. exception object inside 
  2954. the exception handler.<br>
  2955. <br>
  2956.  
  2957. </page>
  2958. <page>
  2959. Another reason not to 
  2960. use exceptions for 
  2961. conventional flow of 
  2962. control is that these 
  2963. "additional" exceptions 
  2964. can get in the way of 
  2965. genuine error-type 
  2966. exceptions. It becomes 
  2967. more difficult for the 
  2968. programmer to keep <br>
  2969.  
  2970. </page>
  2971. <page>
  2972. track of the number of 
  2973. exception cases. For 
  2974. example, when a 
  2975. program processes an 
  2976. excessive variety of 
  2977. exceptions, can we 
  2978. really be sure of just 
  2979. what is being caught by 
  2980. a <b>catch(...)</b>? 
  2981. Exceptional situations <br>
  2982.  
  2983. </page>
  2984. <page>
  2985. should be rare, not 
  2986. commonplace. <br>
  2987. <br>
  2988.  
  2989. </page>
  2990. <page>
  2991. It is best to incorporate 
  2992. your exception-
  2993. handling strategy into a 
  2994. system from the 
  2995. inception of the design 
  2996. process. It is difficult to 
  2997. add effective exception 
  2998. handling after a system 
  2999. has been implemented. <br>
  3000. <br>
  3001.  
  3002. </page>
  3003. <page>
  3004. Use <b>catch(...) </b>to 
  3005. perform recovery that 
  3006. does not depend on the 
  3007. type of the exception, 
  3008. such as releasing 
  3009. common resources. The 
  3010. exception can be 
  3011. rethrown to alert more 
  3012. specific enclosing 
  3013. <b>catch</b> blocks.<br>
  3014.  
  3015. </page>
  3016. <page>
  3017. The ANSI/ISO C++ 
  3018. draft standard 
  3019. recommends that to 
  3020. make programs more 
  3021. robust, programmers 
  3022. should use the version 
  3023. of <b>new</b> that <b>throws</b> 
  3024. <b>bad_alloc</b> exceptions 
  3025. on failure.<br>
  3026. <br>
  3027.  
  3028. </page>
  3029. <page>
  3030. The standard <b>exception</b> 
  3031. hierarchy is meant to 
  3032. serve as a starting 
  3033. point. Users can <b>throw</b> 
  3034. standard exceptions, 
  3035. <b>throw</b> exceptions 
  3036. derived from the 
  3037. standard exceptions or<tt><i> 
  3038. <b>throw</b></i></tt> their own 
  3039. exceptions not derived <br>
  3040.  
  3041. </page>
  3042. <page>
  3043. from the standard 
  3044. exceptions.<br>
  3045. <br>
  3046.  
  3047. </page>
  3048. </section>
  3049. <section type=Popup name=Errors title="Common Errors">
  3050. <page>
  3051. Another reason 
  3052. exceptions can be 
  3053. dangerous as an 
  3054. alternative to normal 
  3055. flow of control is that 
  3056. the stack is unwound 
  3057. and resources allocated 
  3058. prior to the occurrence 
  3059. of the exception may 
  3060. not be freed. This <br>
  3061.  
  3062. </page>
  3063. <page>
  3064. problem can be avoided 
  3065. by careful 
  3066. programming.<br>
  3067. <br>
  3068.  
  3069. </page>
  3070. <page>
  3071. Aborting a program 
  3072. could leave a resource 
  3073. in a state in which other 
  3074. programs would not be 
  3075. able to acquire the 
  3076. resource, hence the 
  3077. program would have a 
  3078. so-called "resource 
  3079. leak." <br>
  3080. <br>
  3081.  
  3082. </page>
  3083. <page>
  3084. It is possible to <b>throw</b> a 
  3085. conditional expression. 
  3086. But be careful because 
  3087. promotion rules may 
  3088. cause the value 
  3089. returned by the 
  3090. conditional expression 
  3091. to be of a different type 
  3092. than you may expect. 
  3093. For example, when <br>
  3094.  
  3095. </page>
  3096. <page>
  3097. throwing an <b>int</b> or a 
  3098. <b>double</b> from the same 
  3099. conditional expression, 
  3100. the conditional 
  3101. expression will convert 
  3102. the <b>int</b> to a <b>double</b>. 
  3103. Therefore the result 
  3104. will always be caught 
  3105. by a <b>catch</b> with a 
  3106. <b>double</b> argument rather <br>
  3107.  
  3108. </page>
  3109. <page>
  3110. than sometimes 
  3111. catching <b>double</b> (for 
  3112. the actual <b>double</b>) and 
  3113. sometimes catching <b>int</b>.<br>
  3114. <br>
  3115.  
  3116. </page>
  3117. <page>
  3118. Exceptions should be 
  3119. thrown only within a 
  3120. <b>try</b> block. An exception 
  3121. thrown outside a <b>try</b> 
  3122. block causes a call to 
  3123. <b>terminate</b>. <br>
  3124. <br>
  3125.  
  3126. </page>
  3127. <page>
  3128. Specifying a comma-
  3129. separated list of <b>catch</b> 
  3130. arguments is a syntax 
  3131. error.<br>
  3132. <br>
  3133.  
  3134. </page>
  3135. <page>
  3136. Placing <b>catch(...) 
  3137. </b>before other <b>catch</b> 
  3138. blocks would prevent 
  3139. those blocks from ever 
  3140. being executed; 
  3141. <b>catch(...) </b>should 
  3142. always be placed last in 
  3143. the list of handlers 
  3144. following a <b>try</b> block.  <br>
  3145. <br>
  3146.  
  3147. </page>
  3148. <page>
  3149. Placing a <b>catch</b> that 
  3150. catches a base class 
  3151. object before a <b>catch</b> 
  3152. that catches an object of 
  3153. a class derived from 
  3154. that base class is a logic 
  3155. error. The base-class 
  3156. <b>catch</b> will catch all 
  3157. objects of classes 
  3158. derived from that base <br>
  3159.  
  3160. </page>
  3161. <page>
  3162. class, so the derived 
  3163. class <b>catch</b> would 
  3164. never be executed.  <br>
  3165. <br>
  3166.  
  3167. </page>
  3168. <page>
  3169. Placing an exception 
  3170. handler with a <b>void *</b> 
  3171. argument type before 
  3172. exception handlers with 
  3173. other pointer types 
  3174. causes a logic error. 
  3175. The <b>void * </b>handler 
  3176. would catch all 
  3177. exceptions of pointer 
  3178. types, so the other <br>
  3179.  
  3180. </page>
  3181. <page>
  3182. handlers would never 
  3183. execute.<br>
  3184. <br>
  3185.  
  3186. </page>
  3187. <page>
  3188. Placing a semicolon 
  3189. after a <b>try</b> block or after 
  3190. any <b>catch</b> handler 
  3191. (other than the last 
  3192. <b>catch</b>) following a <b>try</b> 
  3193. block is a syntax error.<br>
  3194. <br>
  3195.  
  3196. </page>
  3197. <page>
  3198. Assuming that after an 
  3199. exception is processed, 
  3200. control will return to 
  3201. the first statement after 
  3202. the <b>throw</b> is a logic 
  3203. error<br>
  3204. <br>
  3205.  
  3206. </page>
  3207. <page>
  3208. Assuming that an 
  3209. exception thrown from 
  3210. a <b>catch</b> handler will be 
  3211. processed by that 
  3212. handler or any other 
  3213. handler associated with 
  3214. the <b>try</b> block that threw 
  3215. the exception which 
  3216. caused the original <br>
  3217.  
  3218. </page>
  3219. <page>
  3220. <b>catch</b> handler to 
  3221. execute is a logic error.<br>
  3222. <br>
  3223.  
  3224. </page>
  3225. <page>
  3226. Placing an empty 
  3227. <b>throw</b> statement 
  3228. outside a <b>catch</b> handler; 
  3229. executing such a <b>throw</b> 
  3230. causes a call to 
  3231. <b>terminate</b>.<br>
  3232. <br>
  3233.  
  3234. </page>
  3235. <page>
  3236. Throwing an exception 
  3237. not in a function's 
  3238. exception specification 
  3239. causes a call to 
  3240. <b>unexpected</b>.<br>
  3241. <br>
  3242.  
  3243. </page>
  3244. <page>
  3245. User-defined exception 
  3246. classes need not 
  3247. necessarily be derived 
  3248. from class <b>exception</b>. 
  3249. Thus, writing <tt><i><b>catch( 
  3250. exception e )</b></i></tt> is not 
  3251. guaranteed to <b>catch</b> all 
  3252. exceptions a program 
  3253. may encounter.<br>
  3254. <br>
  3255.  
  3256. </page>
  3257. </section>
  3258.  
  3259. <section type=Popup name=Portable title="Portability">
  3260. <page>
  3261. This chapter does not contain any Portability tips.
  3262. </page>
  3263. </section>
  3264. <section type=Popup name=AppletPopup title="Applet Examples">
  3265. <page>
  3266. This chapter does not contain any Applet Examples.
  3267. </page>
  3268. </section>
  3269. </chapter>
  3270. </html>
  3271. </html>
  3272.